简体   繁体   English

复杂的LINQ或EF查询

[英]Complex LINQ or EF query

I have the following 3 classes. 我有以下3个班级。

A workflow configuration has one brand and one workflowtype. 工作流配置具有一个品牌和一个工作流类型。

I need one method in linq or EF that gets me all the brands of existing workflowconfiguration and another method that gets me all the brands of non existing workflow configuration. 我需要linq或EF中的一种方法,它可以获得现有工作流配置的所有品牌,以及另一种让我获得所有品牌的非现有工作流配置的方法。

I am lost cause I dont know where to start. 我迷失了因为我不知道从哪里开始。

public class Brand
    {
        public int BrandId { get; set; }
        public string Name { get; set; }
    }


  public class WorkflowType
    {
        public int WorkflowTypeId { get; set; }
        public string Name { get; set; }
    }

 public class WorkflowConfiguration
    {
        public int WorkflowConfigurationId { get; set; }
        public WorkflowType WorkflowType { get; set; }
        public Brand Brand { get; set; }
        public virtual ICollection<Approver> Approvers { get; set; }

    }

Update1 Here are how my tables would look like and the expected result Update1以下是我的表格的样子和预期结果

Brand

  1. Audi 奥迪

  2. Volkswagen 大众汽车

  3. Mercedes 奔驰

WorkflowTYpes WorkflowTYpes

  1. type 1 1型

  2. type 2 2型

  3. type 3 3型

WorkflowConfiguration WorkflowConfiguration

brandid, workflowtype id brandid,workflowtype id

1 ------------ 1 1 ------------ 1

1 -------------2 1 ------------- 2

List<string> GetBrandsForExistingWorkflowType(string worfklowtype)

If I pass type1 to this method it should return: Audi because for type1, audi exists on the table 如果我将type1传递给此方法,它应返回:Audi因为对于type1,audi存在于表中

List<string> GetBrandsForNonExistingWorkflowType(string workflowType)

If I pass type1 to this method it should return. 如果我将type1传递给此方法,它应该返回。 Volkswagen and Mercedes because for type1, those 2 brands are not in the relationship. 大众和梅赛德斯因为对于1型,那2个品牌都没有关系。

I think this is what you want: 我想这就是你想要的:

List<string> GetBrandsForExistingWorkflowType(string worfklowtype)
{
    var result = db.WorkflowConfigurations
                 .Where(x => x.WorkflowType.Name == worfklowtype)
                 .Select(x => x.Brand);
    return result;
}

List<string> GetBrandsForNonExistingWorkflowType(string workflowType)
{
    var excluded = GetBrandsForExistingWorkflowType(string worfklowtype);
    var result = db.Brands.Except(excluded);
    return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM