【发布时间】:2021-01-21 10:42:26
【问题描述】:
美好的一天。 在动态 Linq 查询方面需要一些帮助。 我有以下 SQL。
Select t.CustomerKey , Count(1)
from dbo.Products p
inner join dbo.Transactions t on t.ProductKey = p.ProductKey
Where p.CategoryLevel3 = 'xxx'
group by t.CustomerKey
having count(1) > 2
我正在使用实体框架。我拥有的 linq 查询给出了相同的结果。
var result2 = entities.Products.Join(entities.Transactions, p => p.ProductKey, t => t.ProductKey, (p, t) => new { p, t })
.Where(z => z.p.CategoryLevel3 == "xxx")
.GroupBy(c => new { c.t.CustomerKey })
.Select(x => new { x.Key.CustomerKey, count = x.Count() })
.Where(y => y.count > 2);
到目前为止,我有以下动态 Linq
var result2 = entities.Products.Join(entities.Transactions, p => p.ProductKey, t => t.ProductKey, (p, t) => new { p, t })
.Where("z => z.p." + filterCondition)
.GroupBy("c => new { c.t." + groupByField + "}")
.Select("x => new (x.Key as CustomerKey, x." + selectfunction + "() as count)")
.Where("y => y.count" + groupByFilterComparisonOperator + groupByFilterInputValue);
我在选择行上遇到语法错误。
filterCondition = "CategoryLevel3 == "xxx""
groupByField ="CustomerKey"
selectfunction =“计数”
groupByFilterComparisonOperator =">"
groupByFilterInputValue = "2"
【问题讨论】:
-
你得到什么语法错误?
-
System.Linq.Dynamic.Core.Exceptions.ParseException: '语法错误'
-
@TheoKoekemoer 您是否尝试过逐步调试它?从具有动态 Where 条件的单个查询开始,然后添加连接等...
-
是的,问题出在 .Select 语句中
-
您的
Select包含语法错误。.Select("x => new (x.Key as CustomerKey, x." + selectfunction + "() as count)")应该是.Select("x => new { CustomerKey = x.Key, Count = x." + selectfunction + "() }")。
标签: c# entity-framework linq group-by dynamic-linq