LINQ 查询适用于 LINQPAD 在 Visual Studio 上抛出错误

本文关键字:出错 错误 Studio Visual 查询 适用于 LINQPAD LINQ | 更新日期: 2023-09-27 18:24:21

var data = (from prod in db.ref_ProductAvail
            group prod by new { prod.storeOfAccount, prod.serviceName } into g
            orderby g.Key.storeOfAccount
            join branch in db.ref_Branch 
            on g.Key.storeOfAccount equals branch.code
            select new
            {
                branchCode = g.Key.storeOfAccount,
                branchName = branch.description,
                serviceName = g.Key.serviceName,
                svcCount = g.Key.serviceName.Count()
            }).ToList();

我的查询在 LINQPAD 上运行,但在 C#>上引发此错误DbExpressionBinding 需要一个具有集合 ResultType.Parameter name: input,

当我寻找解决方案时,我未能找到解决问题的确切解决方案,也删除ToList或使用IEnumerable确实有效,但我无法使用foreach.

LINQ 查询适用于 LINQPAD 在 Visual Studio 上抛出错误

是的,你做对了。 错误应该在g.Key.serviceName.Count(),因为此表达式正在尝试计算字符串serviceName字符数。 这不能转换为 SQL。如果你想实现同样的事情,你可以尝试类似的东西

var data = (from prod in db.ref_ProductAvail
            group prod by new { prod.storeOfAccount, prod.serviceName } into grouping
            orderby grouping.Key.storeOfAccount
            from g in grouping
            join branch in db.ref_Branch 
            on g.Key.storeOfAccount equals branch.code
            select new
            {
                branchCode = g.Key.storeOfAccount,
                branchName = branch.description,
                serviceName = g.Key.serviceName,
                svcCount =  grouping.Select(x=>x.Key.serviceName).Distinct().Count()
            }).ToList();

虽然还没有测试过

相关文章: