Solved

C# - Lamda with tempate

Posted on 2013-06-25
2
259 Views
Last Modified: 2013-06-26
IDictionary<string, string> map =
                            str.Split('|').ToDictionary(s => s.Split('@')[0], s => s.Split('@')[1]);

The above statement works. But I would like to change it to template for IDictionary

public class CSVMap <TKey, TValue>
{
       public IDictionary<TKey, TValue>  func (string str)
      {
IDictionary<TKey, TValue> map =  str.Split('|').ToDictionary
                                           (ConvertValue<TKey>(s => s.Split('@')[0]),
                                            ConvertValue<TValue>(s => s.Split('@')[1]));

         }
        public static T ConvertValue<T>(string value)
        {
            return (T)Convert.ChangeType(value, typeof(T));
        }        
        and ConvertValue to cast the splitted strings to the type of TKey and TValue.

But I got below errors for the ConvertValue portions
error CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type
error CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type

I am not sure what the error mean or how to fix such problem.  Any help will be appreciated. Thanks
0
Comment
Question by:tommym121
2 Comments
 
LVL 23

Accepted Solution

by:
Ioannis Paraskevopoulos earned 500 total points
ID: 39277356
Hi,

The thing you have wrong is that you have s=> in the function parameters, while you should have it outside:

IDictionary<TKey, TValue> map =  str.Split('|').ToDictionary
                                           (s => ConvertValue<TKey>(s.Split('@')[0]), 
                                           s => ConvertValue<TValue>( s.Split('@')[1]));

Open in new window


Giannis
0
 

Author Closing Comment

by:tommym121
ID: 39277496
Thanks
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Moving code from one TFS branch to another 3 33
transaction in asp.net, sql server 6 43
Sort GridView by ID Descending 1 18
Please explain purpose of GZIP 4 37
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question