jeudi 18 février 2016

Adding a new property to the object dynamicaly in a select statement

I need to add a new property to the object into select statement. And I know the name of the property only in runtime. I need something like this:

var propName = "Some name"; // calculated in runtime

// query is IQueryable
query = query.Select(item => new SomeDynamicObject(item)
{
    propName = 1
});

I need to have a copy of the item that has a new property with name which can be calculated in runtime. Moreover the query should be IQueryable because the query should be performed in a database.

I have tried to use ExpandoObject but it works only with IEnumerable:

var propName = "Some name"; // calculated in runtime
query.AsEnumerable().Select(item =>
{
    // ToDynamic create ExpandoObject instanse from existing object
    var expando = item.ToDynamic() as IDictionary<string, object>;
    expando.Add(propName, 1);
    return (dynamic)expando;
});

In the end I want to sort by this new property using Expression.Call().

How can I do that using IQueryable? Please advise.





Aucun commentaire:

Enregistrer un commentaire