简体   繁体   English

使用linq在相关表中选择Entity属性

[英]Select Entity property in related table using linq

here is my setup. 这是我的设置。

Base Model 基本型号

public class Base
{
    public int BaseID { get; set; }
    [StringLength(8)]
    [Index(IsUnique = true)]
    public string BaseNumber { get; set; }
    public ICollection<BillOfMaterial> billOfMaterials { get; set; }
}

BillOfMaterial Model 材料清单模型

public class BillOfMaterial
{
    public int BillOfMaterialID { get; set; }
    [StringLength(10)]
    [Index(IsUnique = true)]
    public string BomNumber { get; set; }
    public ICollection<Base> Bases { get; set; }
}

What I am trying to do is select all bill of material BomNumbers where the base is equal to a input base number. 我想做的是选择所有物料清单BomNumber,其中基数等于输入基数。

What I have tried 我尝试过的

BaseNumber = "A1C1D001";

var BOMQuery = (from Base in db.Bases.Include("BillOfMaterials")
                where Base.BaseNumber == BaseNumber
                select Base.billOfMaterials.ToList());

When trying to create this query I can't see the BomNumber property when I do => select Base.BillOfMaterials.(Can't Find Property) 尝试创建此查询时,当我执行=>选择Base.BillOfMaterials时,看不到BomNumber属性。(找不到属性)

I tried using the .Include() extension to try and bring in the related table in hopes it would give me the property. 我尝试使用.include()扩展名尝试引入相关表,希望它将给我该属性。 Not sure how to word this question exactly to do a good google search for the answer. 不知道如何确切地表达这个问题,以使谷歌搜索一个好的答案。 What am I doing wrong here? 我在这里做错了什么? Any help would be appreciated. 任何帮助,将不胜感激。

Thank you, 谢谢,

When you only need a list of BOMs use the following: 当您只需要BOM表列表时,请使用以下命令:

var BOMQuery = db.Bases
              .Where(x => x.BaseNumber == BaseNumber)
              .SelectMany(a => a.billOfMaterials.Select(b => b.BomNumber)).ToList();

You can then add it to an ObservableCollection like this: 然后可以将其添加到ObservableCollection中,如下所示:

BomList = new ObservableCollection<string>(BOMQuery);

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

相关问题 使用Linq到实体从表和相关表中获取记录 - Get records from table and related related table using Linq to entity 仅使用linq从相关表中选择某些项目 - Only select certain items from related table using linq 为什么在 Linq 的 Select 方法中访问引用实体的属性时,EF Core 会自动加载相关实体? - Why EF Core automatically load related entities when property referring to the entity is accessed in Select method of Linq? 删除与另一个linq实体表相关的记录 - Delete record related to another linq entity table 使用LINQ和EF 4.1填充字典 <S,T> 具有相关实体数据的属性 - Using LINQ and EF 4.1 to populate a Dictionary<S,T> Property with related entity data 使用 LINQ 在 2º 级上层相关实体(一对多)的属性上获取 null - Getting null on property of 2º level upper related entity( one to many) using LINQ 通过在C#中将Linq与相关实体结合使用 - GroupBy using Linq with related Entity in C# 使用linq和相关实体查询实体框架7 - Query to entity framework 7 using linq and related entities 使用linq实体获取基于相关实体的记录 - using linq to entity to get records based on related entity LINQ to SQL手动选择相关的表数据(SELECT N + 1问题) - Manually select related table data (SELECT N + 1 problem) LINQ to SQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM