使用 Lambda 表达式从字段名称中选择不同的字段
我需要从数据库表中获取两个字段(使用 linq-to-sql 检索),一个字段是日期时间(并且是固定字段),另一个字段始终是小数,但该字段可以不同。
该表保存每天以不同货币处理两次的货币数据,因此可能具有 AM_USD、PM_USD、AM_EUR 等字段。我需要获取数据,例如针对 PM_USD 的日期或针对 AM_EUR 的日期列表。
例如,我希望能够使用 lambda 表达式调用数据(这是一个精简的示例):
data = TableData.Select(x=>new {x.DateTimeAdded, x.[**field name as string**]});
我一直在尝试编写一个函数来执行此操作,但失败了。
我管理的最接近的是:
private Func<TableData, KeyValuePair<DateTime, decimal>> CreateSelect(string FieldName)
{
var parameterExp = Expression.Parameter(typeof(TableData), "sel");
var dateParameter = Expression.Parameter(typeof(DateTime), "DateTimeAdded");
var fieldParameter = Expression.Parameter(typeof(decimal), FieldName);
ConstructorInfo constructorInfo = typeof(KeyValuePair<DateTime, decimal>).GetConstructor(new[] { typeof(DateTime), typeof(decimal) });
NewExpression constructExpression = Expression.New(constructorInfo, new ParameterExpression[] { dateParameter, fieldParameter});
var lambda = Expression.Lambda<Func<TableData, KeyValuePair<DateTime, decimal>>>( constructExpression, parameterExp);
return lambda.Compile();
}
失败并显示“System.InvalidOperationException:Lambda 参数不在范围内”。
我确信我错过了一些明显的事情,或者以错误的方式进行。
有什么想法吗?
谢谢 时间
I need to get two fields from a database table (retrieved using linq-to-sql), one field is a datetime (and is a fixed field) and the other is always a decimal, but the field can be different.
The table holds currency data which is processed twice a day and in different currencies so could have fields such as AM_USD, PM_USD, AM_EUR etc. And I need to get data such as a list of the date against PM_USD or the date against AM_EUR.
I would like to be able to call the data using a lambda expression for example (this is a stripped out example):
data = TableData.Select(x=>new {x.DateTimeAdded, x.[**field name as string**]});
I have been trying to write a function to do this, and am failing dismally.
The closest I have managed is:
private Func<TableData, KeyValuePair<DateTime, decimal>> CreateSelect(string FieldName)
{
var parameterExp = Expression.Parameter(typeof(TableData), "sel");
var dateParameter = Expression.Parameter(typeof(DateTime), "DateTimeAdded");
var fieldParameter = Expression.Parameter(typeof(decimal), FieldName);
ConstructorInfo constructorInfo = typeof(KeyValuePair<DateTime, decimal>).GetConstructor(new[] { typeof(DateTime), typeof(decimal) });
NewExpression constructExpression = Expression.New(constructorInfo, new ParameterExpression[] { dateParameter, fieldParameter});
var lambda = Expression.Lambda<Func<TableData, KeyValuePair<DateTime, decimal>>>( constructExpression, parameterExp);
return lambda.Compile();
}
Which fails with "System.InvalidOperationException: Lambda Parameter not in scope".
I'm sure I missing something obvious, or going about it wrong way.
Any ideas?
Thanks
T
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
x.Foo
是x
(属性或字段)的成员,而不是参数:x.Foo
is a member ofx
(property or field), not a parameter:根据您的问题:
可以使用 LINQ 动态查询库。
顺便说一句,即使认为问题是不完全相同,答案 几乎是一样的。
From your question:
Specifying the field name in a LINQ query as a string can be done by using the LINQ Dynamic Query Library.
And by the way, even thought the question isn't exactly identical, the answer is pretty much the same.