首页 > 解决方案 > 有没有办法在实体框架中使用来自不同实体的多个过滤器参数?

问题描述

我正在创建一个事件管理 asp.net 核心应用程序,我的客户实体通过多对多的关系与不同的实体相关。知道我想根据事件名称和主要类别名称实体过滤我的客户。

这是我的实体

顾客

    public class Customer
    {
         public int Id {get; set;}enter code here
         public string Name { get; set; }
         public IList<CustomerEvent> CustomerEvents { get; set; }
         public IList<CustomerMainCategory> CustomerMainCategories { get; set; }
    
    }

事件

public class Event
    {
    public int Id { get; set; }
    public string EventName { get; set; }
    public IList<CustomerEvent> CustomerEvents { get; set; }
   }

客户事件

public Class CustomerEvent
{
        public int CustomerId { get; set; }
        public Customer Customer { get; set; }
        public int EventId { get; set; }
        public Event Event { get; set; }
}

主要类别

public class MainCategory
    {
        public int Id { get; set; }
        public string MainCategoryName { get; set; }
        public IList<CustomerMainCategory> CustomerMainCategories { get; set; }
        
    }

客户主要类别

    public class CustomerMainCategory
    {
        public int CustomerId { get; set; }
        public Customer Customer { get; set; }
        public int MainCategoryId { get; set; }
        public MainCategory MainCategory { get; set; }
    }

我想过滤在指定事件中具有相同主要类别的客户。

先感谢您!!

标签: c#entity-frameworkasp.net-core-webapi

解决方案


像这样的东西?(未测试)


db.Customers.Where(
    c => c.CustomerEvents.Any(ce => ce.Event.Id == x)
    && c.CustomerMainCategories.Any(mc => mc.MainCategory.Id == y)
).ToList();


推荐阅读