首页 > 解决方案 > Linq 哪里过滤

问题描述

查询结果过滤有问题。

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 获取产品

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

解决方案


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

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


推荐阅读