Solved

C# - Lamda with tempate

Posted on 2013-06-25
2
258 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exit the loop 4 43
Counting connections to SQL Server through C# 3 33
Simple Injector with Web Service 4 42
Can I command line build a C# application? 6 33
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 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