Linq哪里过滤

时间:2018-10-05 06:51:33

标签: linq .net-core entity-framework-core

查询结果过滤存在问题。

public class LinkTabProductCategory
{
    [Key]
    public int Id { get; set; }

    public int ProductId { get; set; }
    [JsonIgnore]
    public Product Product { get; set; }

    public int CatalogSubSectionId { get; set; }
    public CatalogSubSection CatalogSubSection { get; set; }
}

var result = DataContext.Product                    
                .Include(o => o.Offers)
                        .ThenInclude(p => p.Prices)
                            .ThenInclude(t => t.Type)
                .Include(p => p.Brand)
                .Include(tb=>tb.LinkTabProductCategories)
                .Where( p=>p.LinkTabProductCategories **???** == id)
                .ToList();

我需要获取具有以下内容的产品列表:LinkTabProductCategories.CatalogSubSectionId == id

更新 这是查询结果列表:

{
    "ProductId":"",
    "UID1C": "",
    "Name": "",
    "Article": "",
    "FactoryNumber": "",
    "Brand": {
        "BrandId": "",
        "UID1C": "",
        "Name": ""
    },
    "Offers": []       
    ,
    "LinkTabProductCategories": [
        {
            "Id": 1,
            "ProductId": 2,
            "CatalogSubSectionId": 1,
            "CatalogSubSection": null
        }
    ]
},
{},
{}.....

如何仅使用“ CatalogSubSectionId” == 1获得产品

2 个答案:

答案 0 :(得分:0)

使用方法Any(predicate)-返回true,任何元素满足条件谓词:

.Where(p => p.LinkTabProductCategories.Any(c => c.CatalogSubSectionId == id))

答案 1 :(得分:0)

条件.Where( p=>p.LinkTabProductCategories **???** == id)是一个列表,因此您必须在列表中搜索CatalogSubSectionId满足给定条件的任何项目。

您可以在此列表中使用Any来提供所需的谓词。

.Where(p => p.LinkTabProductCategories.Any(c => c.CatalogSubSectionId == id))