我有以下 3 个类。
一个工作流程配置有一个品牌和一个工作流程类型。
我需要 linq 或 EF 中的一种方法获取现有工作流配置的所有品牌,另一种方法获取非现有工作流配置的所有品牌。
我迷路了,因为我不知道从哪里开始。
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; }
}
更新1 以下是我的表格的外观和预期结果
品牌
奥迪
大众汽车
奔驰
工作流类型
类型 1
类型 2
类型 3
工作流配置
brandid, workflowtype id
1 ---------- 1
1 --------------2
List<string> GetBrandsForExistingWorkflowType(string worfklowtype)
如果我将 type1 传递给此方法,它应该返回: 奥迪因为对于type1,audi存在于桌面上
List<string> GetBrandsForNonExistingWorkflowType(string workflowType)
如果我将 type1 传递给此方法,它应该返回。 大众和梅赛德斯,因为对于 type1,这两个品牌不在关系中。
最佳答案
我想这就是你想要的:
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;
}
关于linq - 复杂的 LINQ 或 EF 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10794698/