General method for setting property value using expressions / lambda

I'm trying to find a general way to assign values ​​to properties dictated by a lambda expression, see the sample code below, what will the signature for the ConverToEntities method look like and how will 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();
    }
}
+5
source share
3 answers

Ok The closest I could get is:

 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();
        }
    }

It works.

I have a feeling that you are a little confused about what you really want as the return type. It would be great to indicate what we want in a method call or smth. For instance:

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

. , , , , :

this List<long> entities,

.

.

+4

- , . p => p.DataId . Expression , , , :

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

:

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();
}
+2

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

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

and you would call it the following:

ids.ConvertToEntities<Data>( p => new Data { DataId = p } );
+1
source

All Articles