Solved

C# - Lamda with tempate

Posted on 2013-06-25
2
255 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
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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 This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now