我想使用 Linq 過濾 C# 串列中的串列
var catalogs = await _context.MerchantCatalogs
.Include(x => x.merchantDiscounts)
.ToListAsync();
我有一個專案串列,里面有一個折扣串列,我需要為特定用戶獲取折扣
MerchantCatalogs 有一個名為 discount 的欄位,它的折扣型別為 (1,2),我只需要在型別為 2 的串列中獲取折扣
public class MerchantCatalogs
{
int ID;
public ICollection<MerchantDiscount> merchantDiscounts { get; set; }
}
public class MerchantDiscount
{
int id;
int type;
}
如何獲得具有 MerchantDiscount = 1 內部串列的串列
uj5u.com熱心網友回復:
嘗試使用 Where 子句過濾結果:
var catalogs = await _context.MerchantCatalogs
.Include(x => x.merchantDiscounts.Where(discount => discount.type == 2))
.ToListAsync();
如果您只想要有任何型別 2 折扣的商家
var catalogs = await _context.MerchantCatalogs
.Include(x => x.merchantDiscounts.Where(discount => discount.type == 2))
.Where(merchant=> merchant.merchantDiscounts.Any())
.ToListAsync();
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/458837.html
