T>我怎么能对一个IEnumerable&LT创建一个动态的选择;在运行时? [英] How can I create a dynamic Select on an IEnumerable<T> at runtime?

查看:116
本文介绍了T>我怎么能对一个IEnumerable&LT创建一个动态的选择;在运行时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我有

的IEnumerable< T> ,其中 T 的任何对象,我怎么能选择一个?从它特定的属性,因为我知道在运行时作为字符串的属性名称之一的名称



例如:

  VAR externalIEnumerable = DataPassedFromConsumingCode(); //`的IEnumerable< T>`

串knownPropertyName =富;
VAR fooSelect = externalIEnumerable.Select(...);

在本质上,我明明只是做 externalIEnumerable.Select(X = > x.Foo); ,但我需要执行此选择在运行时,当我没有当它最初创建了控制



-



答:基于AlanT的回答,这就是我实际上所做的:

 公共表达式来; Func键< TItem,对象>> SelectExpression< TItem>(串字段名)
{
VAR参数= Expression.Parameter(typeof运算(TItem),项目);
VAR字段= Expression.Property(参数,字段名);
返回Expression.Lambda<&Func键LT; TItem,对象>>(场,
新ParameterExpression [] {参数});
}



我一直是作为一个表达式,因为调用编译引起的IQueryable被枚举,这意味着该数据库不必要的打击。所以,使用它,我只是做了以下内容:

 字符串的PrimaryKey = _map.GetPrimaryKeys(typeof运算(TOriginator))单(); 
VAR primaryKeyExpression = SelectExpression< TOriginator>(PrimaryKey的);
VAR primaryKeyResults = query.Select(pr​​imaryKeyExpression).ToList();


解决方案

这是可能做到这一点使用表达式



如:

 私有类Foo {
酒店的公共串酒吧{搞定;组; }
}

私人的IEnumerable<富> SomeFoos =新的List<富>(){
新富{吧=一月},
新富{吧=二月},
新富{吧=三月 }
新富{吧=四月},
};

[TestMethod的]
公共无效GetDynamicProperty(){

VAR EXPR = SelectExpression<富,串>(酒吧);
VAR propValues​​ = SomeFoos.Select(表达式);

Assert.IsTrue(新[] {一月,二月,月,月} .SequenceEqual(propValues​​));

}

公共静态Func键< TItem,与TFIELD GT; SelectExpression< TItem,与TFIELD GT;(串字段名){

VAR参数= Expression.Parameter(typeof运算(TItem),项目);
VAR字段= Expression.Property(参数,字段名);
返回Expression.Lambda&所述; Func键&下; TItem,TFIELD>>(场,新ParameterExpression [] {参数})编译();

}



心连心,

艾伦。


Given that I have an IEnumerable<T>, where T is any object, how can I select a specific property from it, given that I know the name of the one of the property names at run time as a string?

For example:

var externalIEnumerable = DataPassedFromConsumingCode(); // `IEnumerable<T>`

string knownPropertyName = "Foo";
var fooSelect = externalIEnumerable.Select(...);

In essence, I'm obviously just doing externalIEnumerable.Select(x=> x.Foo);, but I need to perform this Select at runtime, when I don't have control over when it's initially created.

--

ANSWER: Based on AlanT's answer, here's what I actually did:

public Expression<Func<TItem, object>> SelectExpression<TItem>(string fieldName)
{
    var param = Expression.Parameter(typeof(TItem), "item");
    var field = Expression.Property(param, fieldName);
    return Expression.Lambda<Func<TItem, object>>(field, 
        new ParameterExpression[] { param });
}

I kept it as an Expression, because calling Compile caused the IQueryable to be Enumerated, which meant the database was hit unnecessarily. So, to use it, I just do the following:

string primaryKey = _map.GetPrimaryKeys(typeof(TOriginator)).Single();
var primaryKeyExpression = SelectExpression<TOriginator>(primaryKey);
var primaryKeyResults = query.Select(primaryKeyExpression).ToList();

解决方案

It is possible to do this using an Expression

e.g.

private class Foo {
    public string Bar { get; set; }
}

private IEnumerable<Foo> SomeFoos = new List<Foo>() {
    new Foo{Bar = "Jan"},
    new Foo{Bar = "Feb"},
    new Foo{Bar = "Mar"},
    new Foo{Bar = "Apr"},
};

[TestMethod]
public void GetDynamicProperty() {

        var expr = SelectExpression<Foo, string>("Bar");
        var propValues = SomeFoos.Select(expr);

        Assert.IsTrue(new[] { "Jan", "Feb", "Mar", "Apr" }.SequenceEqual(propValues));

    }

public static Func<TItem, TField> SelectExpression<TItem, TField>(string fieldName) {

    var param = Expression.Parameter(typeof(TItem), "item");
    var field = Expression.Property(param, fieldName);
    return Expression.Lambda<Func<TItem, TField>>(field, new ParameterExpression[] { param }).Compile();

}

hth,
Alan.

这篇关于T&GT;我怎么能对一个IEnumerable&LT创建一个动态的选择;在运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆