从字符串列名称动态创建LINQ Select表达式 [英] Creating a LINQ Select expression dynamically from string column names
问题描述
我有一个看起来像这样的方法:
I have a method which looks something like this:
public string GetColVal(string aAcctField, bool callM1, bool callM2)
{
if (callM1)
{
// create select clause here to select a string column with name
// equal to the value of aAcctField from Table1
// Expression<Func<Table1, string>> selector = ?
return M1(selector);
}
if (callM2)
{
// create select clause here to select a string column with name
// equal to the value of aAcctField from Table2
// Expression<Func<Table2, string>> selector = ?
return M2(selector);
}
}
M1()
是这样的:
public string M1(Expression<Func<Table1, string>> columnToSelect, string xType)
{
// other logic
string acct = _cntx.Where(c => c.Type == xType)
.Select(columnToSelect)
.FirstOrDefault();
return acct;
}
M2()
也类似.请注意,方法过于简单.方法M1()
和M2()
可以完美地工作.我可以这样调用它们:
M2()
is also something similar. Please note that the methods are over-simplified. And the methods M1()
and M2()
work perfectly. I can invoke them this way:
// MyColumn is a strongly typed column in Table1
string acct = M1(x => x.MyColumn, "some value");
但是,在方法GetColVal()
中如何构造选择子句?关于selector
的评论将帮助您了解我打算做什么.所以,请继续阅读评论.
But, inside the method GetColVal()
how do I construct the select clauses? The comments regarding selector
will help you understand what I intend to do. So, please go ahead and read the comment.
我已经尝试过了:
public string GetColVal(string aAcctField, bool callM1, bool callM2)
{
if (callM1)
{
// create select clause here to select a string column with name
// equal to the value of aAcctField from Table1
Expression<Func<Table1, string>> selector = (w) => w.GetType().GetProperty(aAcctField).Name;
return M1(selector);
}
...
}
我得到一个例外:
LINQ to Entities无法识别该方法 'System.Reflection.PropertyInfo GetProperty(System.String)'方法, 而且该方法无法转换为商店表达式
LINQ to Entities does not recognize the method 'System.Reflection.PropertyInfo GetProperty(System.String)' method, and this method cannot be translated into a store expression
我看过这些:
- Create dynamic LINQ expression for Select with FirstOrDefault inside
- Linq access property by variable
- Get property value from string using reflection in C#
- and many others.
但是它们都不是我所需要的.
but none of them is quite something like what I need.
推荐答案
基本上,您需要使用 Expression.Lambda
,
Basically you need to use the Expression
class methods like Expression.Lambda
, Expression.PropertyOrField
etc. to build the desired selector like:
static Expression<Func<T, TValue>> MemberSelector<T, TValue>(string name)
{
var parameter = Expression.Parameter(typeof(T), "item");
var body = Expression.PropertyOrField(parameter, name);
return Expression.Lambda<Func<T, TValue>>(body, parameter);
}
要支持嵌套属性,请将var body = ...
行更改为
To support nested properties, change var body = ...
line to
var body = name.Split('.').Aggregate((Expression)parameter, Expression.PropertyOrField);
样品用量:
if (callM1)
{
return M1(MemberSelector<Table1, string>(aAcctField));
}
...
这篇关于从字符串列名称动态创建LINQ Select表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!