Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# - Lamda with tempate

Posted on 2013-06-25
2
Medium Priority
?
264 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

926 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