[英]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.