【发布时间】:2018-07-06 10:11:36
【问题描述】:
我需要动态构建查询(实体类有 30 多个属性,我想避免使用多个 if 语句)。 我使用反射和表达式树解决了这个问题(如果指定了某些搜索条件,则按属性迭代并附加谓词)。 问题是它会生成原始 SQL 脚本。
例子
SELECT
[Extent1].[ProductId] AS [ProductId],
[Extent1].[ManufacturerId] AS [ManufacturerId],
[Extent1].[Title] AS [Title]
FROM [dbo].[Products] AS [Extent1]
WHERE 3 = [Extent1].[ManufacturerId]
我需要使用 sp_executesql 过程获取查询(继续执行相同的查询执行计划)。
exec sp_executesql N'SELECT
[Extent1].[ProductId] AS [ProductId],
[Extent1].[ManufacturerId] AS [ManufacturerId],
[Extent1].[Title] AS [Title]
FROM [dbo].[Products] AS [Extent1]
WHERE [Extent1].[ManufacturerId] = @p__linq__0',N'@p__linq__0 int',@p__linq__0=3
原来问题出在下面的方法中。它使用 Expression.Constant 创建 Expression 对象。我不知道我应该用什么替换它。
public static Expression<Func<TItem, bool>> PropertyEqual<TItem, TValue>(
this PropertyInfo property, TValue value)
{
var param = Expression.Parameter(typeof(TItem));
var body = Expression.Equal(Expression.Property(param, property),
Expression.Constant(value, typeof(TValue)));
return Expression.Lambda<Func<TItem, bool>>(body, param);
}
我该如何解决这个问题?
【问题讨论】:
-
表达式应该是等价的。也许这就是您使用它的方式...您使用的是什么
.Where重载? -
Where
(this IQueryable source, Expression > predicate)
标签: c# entity-framework linq expression