Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C# - Lamda with tempate

Posted on 2013-06-25
2
Medium Priority
?
263 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
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…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 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