鉴于我有一个IEnumerable<T>,在哪里T有任何对象,我如何从中选择一个特定属性,因为我知道运行时其中一个属性名称的名称是一个字符串?
例如:
var externalIEnumerable = DataPassedFromConsumingCode(); // `IEnumerable<T>`
string knownPropertyName = "Foo";
var fooSelect = externalIEnumerable.Select(...);
Run Code Online (Sandbox Code Playgroud)
本质上,我显然只是在做externalIEnumerable.Select(x=> x.Foo);,但我需要Select在运行时执行此操作,此时我无法控制它最初创建的时间.
-
答案:根据AlanT的回答,这是我实际做的:
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 });
}
Run Code Online (Sandbox Code Playgroud)
我将它保存为Expression,因为调用Compile导致IQueryable被枚举,这意味着数据库被不必要地命中.所以,要使用它,我只需执行以下操作:
string primaryKey = _map.GetPrimaryKeys(typeof(TOriginator)).Single();
var primaryKeyExpression = SelectExpression<TOriginator>(primaryKey);
var primaryKeyResults = query.Select(primaryKeyExpression).ToList();
Run Code Online (Sandbox Code Playgroud) 要动态生成GroupBy表达式,我正在尝试构建Linq表达式树.要分组的字段是动态的,可以在数量上有所不同.
我用这个代码:
string[] fields = {"Name", "Test_Result"};
Type studentType = typeof(Student);
var itemParam = Expression.Parameter(studentType, "x");
var addMethod = typeof(Dictionary<string, object>).GetMethod(
"Add", new[] { typeof(string), typeof(object) });
var selector = Expression.ListInit(
Expression.New(typeof(Dictionary<string,object>)),
fields.Select(field => Expression.ElementInit(addMethod,
Expression.Constant(field),
Expression.Convert(
Expression.PropertyOrField(itemParam, field),
typeof(object)
)
)));
var lambda = Expression.Lambda<Func<Student, Dictionary<string,object>>>(
selector, itemParam);
Run Code Online (Sandbox Code Playgroud)
代码是从这篇文章复制而来的(感谢Mark Gravel!).
最终确定......
var currentItemFields = students.Select(lambda.Compile());
Run Code Online (Sandbox Code Playgroud)
......我预计我可以把它改成......
var currentItemFields = students.GroupBy(lambda.Compile());
Run Code Online (Sandbox Code Playgroud)
我认为lambda表达只不过是......
var currentItemFields = students.GroupBy(o => new { o.Name, o.Test_Result });
Run Code Online (Sandbox Code Playgroud)
......但不幸的是,情况似乎并非如此.具有动态lambda的GroupBy不会给出任何异常,它只是不对任何内容进行分组并返回所有元素.
我在这做错了什么?任何帮助,将不胜感激.提前致谢.