Generic method to set the value of a property using expressions/lambda
Asked Answered
P

3

5

I am trying to find a generic way to assign values to a property dictated by a lambda expression, look at the example code below, how would the signature for the ConverToEntities method look and how would it be called?

static void Main()
{
    List<long> ids = new List<long> {1, 2, 3};

    //Non generic way
    List<Data> dataItems = ids.ConvertToDataItems();

    //Generic attempt!!
    List<Data> differntDataItems =
        ids.ConvertToEntities<Data>( p => p.DataId );
}

public class Data
{
    public long DataId;
    public string Name;
}

public static class ExtensionMethods
{
    public static List<Data> ConvertToDataItems(this List<long> dataIds)
    {
        return dataIds.Select(p => new Data { DataId = p }).ToList();
    }

    public static List<T> ConvertToEntities<TProp>(
        this List<long> entities, Func<TProp> lambdaProperty )
    {
        return entities.Select(p => new T {lambdaProperty} ).ToList();
    }
}
Patchouli answered 14/2, 2012 at 1:29 Comment(3)
What is List<T> tho? .. I guess what you actually want is public static List<TProp> ConvertToEntities<TProp> ... or not?Fairbanks
The ConvertToEntities is an attempt at a generic method; it is syntax incorrect, I am looking to make this method generic;Patchouli
Given the solutions, the .Select provided with .Net is less code...Tilth
F
4

Ok. The closest I could get was this :

 class Program
    {
        static void Main(string[] args)
        {
            List<long> ids = new List<long> { 1, 2, 3 };

            //Non generic way
            List<Data> dataItems = ids.ConvertToDataItems();

            //Generic attempt!!

            Func<long, Data> selector = (p => new Data { DataId = p });
            List<Data> differntDataItems = ids.ConvertToEntities<Data>(selector);
        }
    }

    public class Data
    {
        public long DataId;
        public string Name;
    }

    public static class ExtensionMethods
    {
        public static List<Data> ConvertToDataItems(this List<long> dataIds)
        {
            return dataIds.Select(p => new Data { DataId = p }).ToList();
        }

        public static List<TProp> ConvertToEntities<TProp>(this List<long> entities, Func<long, TProp> selector)
        {
            return entities.Select(selector).ToList();
        }
    }

This works.

I have the feeling you got urself a little confused with what you actually want as the return type. It would be cool to be able to specify what we want in the method call or smth. For example:

    public static List<TProp> ConvertToEntities<T, TProp>(List<T> entities, Func<T, TProp> selector)
    {
        return entities.Select(selector).ToList();
    }

This provides us more flexibility on the return type. But since we are doing this using extensions, I assume this is impractical because we need to know what type we are extending:

this List<long> entities,

Nice question.

EDIT Code suggestion fix.

Fairbanks answered 14/2, 2012 at 1:57 Comment(1)
thanks man. and thank you, it was a good riddle for us newbiesFairbanks
A
2

You can do something like this, but it's not as simple or nice. The lambda p => p.DataId gives you the get accessor of the property. You could use Expressions to get the setter, but it's probably better to use the setter directly in the lambda:

List<Data> differntDataItems =
    ids.ConvertToEntities<long, Data>((p, i) => p.DataId = i);

The implementation would look like this:

public static List<T> ConvertToEntities<TProp, T>(
    this List<TProp> dataIds, Action<T, TProp> lambdaProperty)
    where T : new()
{
    return dataIds.Select(
        p =>
        {
            var result = new T();
            lambdaProperty(result, p);
            return result;
        }).ToList();
}
Acetyl answered 14/2, 2012 at 2:1 Comment(0)
E
1

I believe @Zortkun is right about the return type. Try the followin:

public static List<TProp> ConvertToEntities<TProp>(
    this List<long> entities, Func<long, TProp> lambdaProperty )
{
    return entities.Select(lambdaProperty).ToList();
}

and you would call it as follows:

ids.ConvertToEntities<Data>( p => new Data { DataId = p } );
Estivation answered 14/2, 2012 at 1:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.