过滤相关实体

问题描述 投票:0回答:1

如何过滤和检索相关实体中所需的项目?

例如。如果 PurchasingTrans 有 3 种品牌的商品,而我只需要 1 种类型..现在它会返回所有 3 个品牌,即使其中 1 个类似于查询品牌。

     var ressales = await _dbcontext.s_purmas
    .Include(c => c.PurchaseTrans)
    .ThenInclude(c => c.Item)
    .Where(r=> r.sdate >= dtfrom && r.sdate <= dtto)
    .Where(r=> r.cancel == "0")
    .Where(p => p.PurchaseTrans.Any(t => EF.Functions.Like(t.Item.brand_name, brand)))
    .OrderBy(r => r.id)
    .ToListAsync();

我尝试过改变。任何。全部,但仍然不起作用

编辑:

这也不起作用: .Where(p => p.PurchaseTrans.Any(t => t.Item.brand_name == 品牌))

班级

   public class PurchaseMaster
   {
    public int id { get; set; }
    public DateOnly? sdate { get; set; }  

    public DateOnly? pur_date { get; set; }  
    public string refno { get; set; } = string.Empty;
    public string? sup_code { get; set; }
    public string? sup_name { get; set; }

    public string? lcno { get; set; }
    public string? veno { get; set; }
    public string? ordno { get; set; }
    public string? cancel { get; set; }
    public virtual ICollection<PurchaseTran> PurchaseTrans { 
     get;set; }  = new List<PurchaseTran>();
   }

   // Dependent (child)
   public class PurchaseTran
   {
    public int id { get; set; }
    public string brand { get; set; }
    public string stk_no { get; set; } 
    public string refno { get; set; } 
    public double cost { get; set; }

    public double rec_qty { get; set; }
    public int? vat { get; set; }

    public double? vatrate { get; set; }
    public int purchaseid { get; set; } // Required foreign key property

    public int itemId { get; set; } 
    public virtual PurchaseMaster PurchaseMaster { get; set; } = null; // Required reference navigation to principal

    public virtual Item Item { get; set; }

   }
c# .net entity-framework
1个回答
0
投票

我如何过滤和检索相关项目中所需的项目 实体?

例如。如果PurchaseTrans有3种品牌的商品并且我需要 只有 1 种类型..现在它会返回所有 3 个品牌,即使其中 1 个是 喜欢查询品牌。

根据您当前的代码片段和描述,我尝试重现您的问题,我发现,

EF.Functions.Like(t.Item.brand_name, brand))
检查PurchaseMaster中是否至少有一个PurchaseTran具有与查询匹配的品牌。这将返回所有拥有任何具有匹配品牌的PurchaseTrans的PurchaseMaster,即使还有其他具有不同品牌的PurchaseMaster。

相反,我尝试了以下方法:

var purchaseMasters = new List<PurchaseMaster>
{
    new PurchaseMaster
    {
        id = 1,
        sdate = new DateOnly(2024, 4, 1),
        cancel = "0",
        PurchaseTrans = new List<PurchaseTran>
        {
            new PurchaseTran { id = 1, brand = "BrandA", Item = new Item { brand_name = "BrandA" } },
            new PurchaseTran { id = 2, brand = "BrandB", Item = new Item { brand_name = "BrandB" } }
        }
    },
    new PurchaseMaster
    {
        id = 2,
        sdate = new DateOnly(2024, 4, 2),
        cancel = "1",
        PurchaseTrans = new List<PurchaseTran>
        {
            new PurchaseTran { id = 3, brand = "BrandC", Item = new Item { brand_name = "BrandC" } },
            new PurchaseTran { id = 4, brand = "BrandA", Item = new Item { brand_name = "BrandA" } }
        }
    }
};

    string brand = "BrandA"; 
    DateTime dtfrom = new DateTime(2024, 4, 1); 
    DateTime dtto = new DateTime(2024, 4, 30); 

    var ressales = purchaseMasters
        //.Where(pm => pm.sdate >= dtfrom && pm.sdate <= dtto)
        .Where(pm => pm.cancel == "0")
        .Where(pm => pm.PurchaseTrans.Any(pt => pt.brand == brand))
        .OrderBy(pm => pm.id)
        .ToList();

    //Checking Output the results in local console
    foreach (var purchase in ressales)
    {
        Console.WriteLine($"Purchase ID: {purchase.id}");

        foreach (var purchaseTrans in purchase.PurchaseTrans)
        {
            Console.WriteLine($" - Transaction ID: {purchaseTrans.id}, Brand: {purchaseTrans.brand}");
        }
    }

根据我的测试,修改后的查询应该打印出购买 ID 以及关联的供应商名称,并且对于该购买中的每笔交易,它将打印出交易 ID 和关联项目的品牌,其取消状态等于

"0" 
.

因此,您可以使用

.Any(pt => pt.brand == brand))
代替
Any(t => EF.Functions.Like

输出:

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.