使用defaultifempty()在linq查询中使用Group的错误
我有以下LINQ(到SQL Server)查询:
var railcarsByProduct = await (from r in DbContext.Railcars
let p = DbContext.ProductAliases
.Where(pa => pa.Product.Company.CompanyCode == companyCode && pa.Alias == r.Product)
.Select(pa => pa.Product.Name)
.FirstOrDefault()
where r.Facility.Company.CompanyCode == companyCode && r.Departure == null
group r by p into productGroup
select new { Product = productGroup.Key, Count = productGroup.Count() }
).ToListAsync();
这很好。但是,有可能productaliases
子查询将返回null。在这种情况下,我想默认为R.Product
。
我尝试将defaultifempty()
添加到子查询中。
var railcarsByProduct = await (from r in DbContext.Railcars
let p = DbContext.ProductAliases
.Where(pa => pa.Product.Company.CompanyCode == companyCode && pa.Alias == r.Product)
.Select(pa => pa.Product.Name)
.DefaultIfEmpty(r.Product)
.FirstOrDefault()
where r.Facility.Company.CompanyCode == companyCode && r.Departure == null
group r by p into productGroup
select new { Product = productGroup.Key, Count = productGroup.Count() }
).ToListAsync();
但这给出了一个错误。
'linq表达式'dbset() 。 .select(pa => pa.product.name) 。可以通过插入“可忽略的”,“ asasyncenumerable”,“ tolist”或“ tolistAsync'的呼叫来重写可以翻译的形式的查询,或者明确切换到客户端评估。
接下来,我尝试通过子句在组中处理无效案例。
var railcarsByProduct = await (from r in DbContext.Railcars
let p = DbContext.ProductAliases
.Where(pa => pa.Product.Company.CompanyCode == companyCode && pa.Alias == r.Product)
.Select(pa => pa.Product.Name)
.FirstOrDefault()
where r.Facility.Company.CompanyCode == companyCode && r.Departure == null
group r by p ?? r.Product into productGroup
select new { Product = productGroup.Key, Count = productGroup.Count() }
).ToListAsync();
但这也给出了完全相同的错误。
我知道可以将所有行放下,然后将它们分组为C#代码。但是,有人看到了我不需要这样做的方法吗?
I have the following LINQ (to SQL Server) query:
var railcarsByProduct = await (from r in DbContext.Railcars
let p = DbContext.ProductAliases
.Where(pa => pa.Product.Company.CompanyCode == companyCode && pa.Alias == r.Product)
.Select(pa => pa.Product.Name)
.FirstOrDefault()
where r.Facility.Company.CompanyCode == companyCode && r.Departure == null
group r by p into productGroup
select new { Product = productGroup.Key, Count = productGroup.Count() }
).ToListAsync();
This is working fine. However, there is the possibility that the ProductAliases
subquery will return null. In that case, I want to default to r.Product
.
I tried adding DefaultIfEmpty()
to the subquery.
var railcarsByProduct = await (from r in DbContext.Railcars
let p = DbContext.ProductAliases
.Where(pa => pa.Product.Company.CompanyCode == companyCode && pa.Alias == r.Product)
.Select(pa => pa.Product.Name)
.DefaultIfEmpty(r.Product)
.FirstOrDefault()
where r.Facility.Company.CompanyCode == companyCode && r.Departure == null
group r by p into productGroup
select new { Product = productGroup.Key, Count = productGroup.Count() }
).ToListAsync();
But this gives an error.
'The LINQ expression 'DbSet()
.Where(pa => pa.Product.Company.CompanyCode == __companyCode_0 && pa.Alias == r.Outer.Outer.Product)
.Select(pa => pa.Product.Name)
.DefaultIfEmpty(r.Outer.Outer.Product)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'.
Next, I tried handling the null case within the group by
clause.
var railcarsByProduct = await (from r in DbContext.Railcars
let p = DbContext.ProductAliases
.Where(pa => pa.Product.Company.CompanyCode == companyCode && pa.Alias == r.Product)
.Select(pa => pa.Product.Name)
.FirstOrDefault()
where r.Facility.Company.CompanyCode == companyCode && r.Departure == null
group r by p ?? r.Product into productGroup
select new { Product = productGroup.Key, Count = productGroup.Count() }
).ToListAsync();
But this also gives the exact same error.
I understand could just bring down all the rows and then group them in C# code. But does anyone see a way where I wouldn't need to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用以下类似的内容。
不确定它是否会起作用,但我知道过去的默认设备很奇怪,或者过去对实体框架很奇怪。
Try using something like the below.
Not sure if it will work but I know the DefaultIfEmpty is weird or has been weird with Entity Framework in the past.
尝试以下查询:
Try the following query: