![](/img/trans.png)
[英]Rebuild Parent->Child->GrandChild hierarchy with LINQ performance
[英]Join Parent, Child and GrandChild tables with latest record from grandchild table using LinQ in C#
請注意,這只是為了模擬示例,而不是實際的表本身。
很抱歉的道歉
我有三個表供應商,產品和訂單。 它們的鏈接如下。
供應商-> ID,名稱,位置(此表包含有關供應商的所有信息)
產品-> ID,供應商ID,名稱,說明(此表包含供應商出售的所有產品)
訂單-> ID,ProductId,Date(此表包含該產品的所有訂單)
供應商與“產品”表具有一對一關系,但某些供應商可能根本沒有產品。
產品表與訂單表具有一對多關系
我正在嘗試使用linq獲取最新的供應商訂單。
例。
供應商:
1 | Microsoft | Seattle
2 | Apple | California
3 | Amazon | Seattle
產品名稱:
1 | 1 | MS Office | Office Suite
2 | 2 | iPhone | Smart Phone
訂購:
1 | 1 | 05/27/2016
2 | 1 | 06/07/2016
3 | 2 | 04/17/2016
4 | 2 | 06/01/2016
預期結果:
1 | Microsoft | Seattle
1 | 1 | MS Office | Office Suite
2 | 1 | 06/07/2016
2 | Apple | California
2 | 2 | iPhone | Smart Phone
4 | 2 | 06/01/2016
3 | Amazon | Seattle
null
null
var vendor = db.Vendor.Include(x => x.Product.Select(s => s.Order)).OrderBy(a => a.Name).Select(s => new VendorModel
{
Id = c.Id,
Name = c.Name,
Location = c.Location
Product = c.Product.Select(m => new ProductModel
{
Id = m.Id,
VendorId = m.VendorId,
Name = m.Name,
Description = m.Description,
}).FirstOrDefault(),
Order = c.Order.Select(m => new OrderModel
{
Id = m.Id,
ProductId = m.ProductId,
Date = m.Date,
}).OrderBy(o=>o.Date).FirstOrDefault(),
});
public class VendorModel
{
public ApplicationModel() { }
public ApplicationModel(int id, string name, string location)
{
this.Id = id;
this.Name = name;
this.Location = location;
}
public int Id { get; set; }
public string Name { get; set; }
public string Location{ get; set; }
public virtual ProductModel Product { get; set; }
public virtual OrderModel Order { get; set; }
}
public class ProductModel
{
public ProductModel() { }
public ProductModel(int id, int vendorId, string name, string description)
{
this.Id = id;
this.VendorId = vendorId;
this.Name = name;
this.Description = description;
}
public int Id { get; set; }
public int VendorId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public VendorModel Vendor { get; set; }
}
public class OrderModel
{
public OrderModel() { }
public OrderModel(int id, int productId, DateTime date)
{
this.Id = id;
this.ProductId = productId;
this.Date = date;
}
public int Id { get; set; }
public int ProductId { get; set; }
public DateTime Date { get; set; }
public ProductModel Product { get; set; }
}
由於供應商可能具有空值,因此我認為您應該使用左外部聯接。 這是如何使用LINQ實現左外部聯接。
var query = from v in Vendor
join p in Product on v.id equals p.vendorId into t1
from vp in t1.DefaultIfEmpty()
join o in Orders on p.id equals o.productId t2
from vpo in t2.DefaultIfEmpty()
orderby v.Name, vpo.date
select new
{
v.Name, v.Location,
productId=(int?)vp.id, // To handle null value if id(ProductId) is specified as not null in Product table.
vp.Name, vp.Description,
orderId = (int?)vpo.id, // To handle null value if id(OrderId) is specified as not null in Orders table.
vpo.date
}.ToList();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.