[英]Filter list by grandchild using LINQ
I need to filter a list by the DamageCodeName field in the DamageCode class.我需要通过 DamageCode class 中的 DamageCodeName 字段过滤列表。
public partial class DamageCategory
{
public string DamageCategoryId { get; set; }
public string CategoryName { get; set; }
}
public partial class DamageGroup
{
public string DamageGroupId { get; set; }
public string DamageCategoryId { get; set; }
public string GroupName { get; set; }
}
public partial class DamageCode
{
public string DamageCodeId { get; set; }
public string DamageGroupId { get; set; }
public string DamageCodeName { get; set; }
}
I pull the records using EF CORE 5 into a list:我使用 EF CORE 5 将记录拉到一个列表中:
private List<DamageCategory> _DamageCodeList { get; set; } = new();
_DamageCodeList = _contextDB.DamageCategories
.Include(i => i.DamageGroups)
.ThenInclude(d => d.DamageCodes).AsSingleQuery().ToListAsync();
Now I need to filter this list by the DamageCode.DamageCodeName property.现在我需要通过 DamageCode.DamageCodeName 属性过滤这个列表。
private string _SearchText { get; set; } = "Bubble";
private List<DamageCategory> _CategoryList { get; set; } = new();
_CategoryList = _DamageCodeList.Where(g => g.DamageGroups.SelectMany(c => c.DamageCodes
.Where(w => w.DamageCodeName.ToLower().Contains(_SearchText.ToLower()))).Any()).ToList();
The code above only filters for the DamageCategory.上面的代码只过滤了 DamageCategory。 It brings back all the records for the DamageGroup and all the records for the DamageCodes.
它带回了 DamageGroup 的所有记录和 DamageCodes 的所有记录。
I need the linq query result to produce a list like the one below (Filtered by "Bubble") and bring back only the DamageCategory, DamageGroup, and DamageCodes filtered by DamageCode.DamageCodeName.Contains("Bubble"):我需要 linq 查询结果来生成如下列表(按“气泡”过滤),并只带回由 DamageCode.DamageCodeName.Contains(“Bubble”)过滤的 DamageCategory、DamageGroup 和 DamageCodes:
Here is the SQL that produces the result above that I need:这是产生上面我需要的结果的 SQL:
SELECT
CT.[DamageCategoryID],
CT.[CategoryName],
DG.[DamageGroupID],
DG.[DamageCategoryID],
DG.[GroupName],
DC.[DamageCodeID],
DC.[DamageGroupID],
DC.[DamageCodeName]
FROM
[dbo].[DamageCategory] AS CT
INNER JOIN [dbo].[DamageGroup] AS DG ON CT.[DamageCategoryID] = DG.[DamageCategoryID]
INNER JOIN [dbo].[DamageCode] AS DC ON DG.[DamageGroupID] = DC.[DamageGroupID]
WHERE
DC.[DamageCodeName] LIKE '%Bubble%'
This is where query syntax shines.这就是查询语法大放异彩的地方。
from dc in _contextDB.DamageCategories
from dg in dc.DamageGroups
from dc in dg.DamageCodes
where dc.DamageCodeName.Contains("Bubble")
select new
{
dc.DamageCategoryID,
dc.CategoryName,
dg.DamageGroupID,
dg.DamageCategoryID,
dg.GroupName,
dc.DamageCodeID,
dc.DamageGroupID,
dc.DamageCodeName
}
The query shape from... from
is the query syntax equivalent of SelectMany
.查询形状
from... from
是SelectMany
的查询语法等价物。
You use ToLower
in your code.您在代码中使用
ToLower
。 That may not be necessary.那可能没有必要。 The query is translated into SQL and if the database field has a case-insensitive collation you don't need
ToLower
.查询被翻译成 SQL 并且如果数据库字段具有不区分大小写的排序规则,则不需要
ToLower
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.