我嘗試獲取特定位置的專案摘要。我嘗試對它們進行分組,因為一個專案可以在特定位置多次出現并獲取該專案的摘要。它適用于 ITEMID 和 REMAIN 但當我嘗試訪問描述時它失敗了我的命令看起來像這樣
var items1 = await _context.Set<Comitemtran>()
.Include(c => c.Bzitem)?
.Include(c => c.Bzchopp)?
.Include(c => c.Bzitemlot)?
.Include(c => c.Bzstoreplace)?
.Where(c => c.Bzstoreplaceid == placecode).GroupBy(c => c.Bzitemid).Select(group => new ItemTransView
{
BZITEMID = group.Key,
BZDESC = group.Where(c => c.Bzitemid == group.Key).FirstOrDefault().Bzitem.Bzdesc,
BZREMAIN = group.Sum(s => s.Bzquantity)
}).ToListAsync();
當我執行它時,我在郵遞員上得到這個回復:
System.InvalidOperationException:LINQ 運算式 'GroupByShaperExpression:KeySelector:c.BZITEMID,ElementSelector:EntityShaperExpression:EntityType:Comitemtran ValueBufferExpression:ProjectionBindingExpression:EmptyProjectionMember IsNullable:False .Select(s => s.Bzitem.Bzdesc).First()' 不能被翻譯。以可翻譯的形式重寫查詢,或通過插入對“AsEnumerable”、“AsAsyncEnumerable”、“ToList”或“ToListAsync”的呼叫顯式切換到客戶端評估。
uj5u.com熱心網友回復:
添加Description到分組鍵。在這種情況下,查詢將被轉換為 SQL
var items1 = await _context.Set<Comitemtran>()
.Where(c => c.Bzstoreplaceid == placecode)
.GroupBy(c => new { c.Bzitemid, c.Bzitem.Bzdesc })
.Select(group => new ItemTransView
{
BZITEMID = group.Key.Bzitemid,
BZDESC = group.Key.Bzdesc,
BZREMAIN = group.Sum(s => s.Bzquantity)
})
.ToListAsync();
請注意,Include后面跟 . 時會忽略 's Select。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/449541.html
