带有Include语句的实体框架,用于选择第3级特定字段

时间:2017-03-22 18:18:21

标签: c# entity-framework

我搜索了很多相似的案例,但没有运气。

我的数据驻留在SQL Server 2014和针对EF版本运行的应用程序:6。

以下是模型结构/关系:

Supplier -- 1:m -- Receipt -- 1:m -- ReceiptItem -- m:1 -- Item

模型类:

public partial class Supplier
{
    public Supplier()
    {
        this.Receipts = new HashSet<Receipt>();
    }

    public int supplierId { get; set; }
    public string supplierName { get; set; }
    public string supplierPhone { get; set; }
    public string supplierAddress { get; set; }
    public string supplierRemark { get; set; }

    public virtual ICollection<Receipt> Receipts { get; set; }
}

public partial class Receipt
{
    public Receipt()
    {
        this.ReceiptItems = new HashSet<ReceiptItem>();
    }

    public int receiptId { get; set; }
    public int supplierId { get; set; }
    public Nullable<System.DateTime> receiptDate { get; set; }

    public virtual Supplier Supplier { get; set; }
    public virtual ICollection<ReceiptItem> ReceiptItems { get; set; }
}

public partial class ReceiptItem
{
    public int receiptId { get; set; }
    public int itemId { get; set; }
    public short itemQnty { get; set; }
    public double itemCost { get; set; }  

    public virtual Item Item { get; set; }
    public virtual Receipt Receipt { get; set; }
}

public partial class Item
{
    public Item()
    {
        this.ReceiptItems = new HashSet<ReceiptItem>();
    }

    public int itemId { get; set; }
    public string itemName { get; set; }
    public string itemPackage { get; set; }
    public string itemCategory { get; set; }

    public virtual ICollection<ReceiptItem> ReceiptItems { get; set; }
}

所需的输出是:

 {"receiptId": 9,
"supplierId": 1,
"supplierName": “SomeOne”, // << to be grabbed from the (Supplier) entity
"receiptDate": "2017-03-05T17:54:49.573",
"ReceiptItems": [{
    "itemId": 1,
    "receiptId": 9,
    "itemName": "item A",  // << to be grabbed from the (Item) entity
    "itemQnty": 4,
    "itemCost": 30.0
  },{
    "itemId": 2,
    "receiptId": 9,
    "itemName": "item B",  // << to be grabbed from the (Item) entity
    "itemQnty": 7,
    "itemCost": 50.0
  }]
}

上下文已禁用延迟加载(Configuration.LazyLoadingEnabled = false;)。我正在尝试执行以下操作:

var receipt = await db.Receipts
                      .Where(r => r.receiptId.Equals(intQryTerm))
                      .Include(r => r.Supplier)
                      .Include(r => r.ReceiptItems)
                      .Include(r => r.ReceiptItems.Select(receiptItem => receiptItem.Item)) 
                      .FirstOrDefaultAsync();

但它让我一团糟! (很多子级别)。

我需要的是与上面所示相同的结构(没有更多的继承)。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

五天过去了,没有回答! 与此同时,我花了几个小时寻找可以提供帮助的资源,并尝试了很多技巧...... 虽然问题要求Include语句,但由于所需的输出不会影响模型类(Receipt包含额外的属性&#34; supplierName和ReceiptItem包含额外的属性itemName),我感到惊讶的是代码是多么容易和干净甚至< strong>不使用&#34; Include&#34;声明即可。
这是代码:

var receipt = await db.Receipts
        .Where(r => r.receiptId.Equals(receiptId))
        .Select(r => new
            {
                r.receiptId,
                r.receiptDate,
                r.supplierId,
                r.Supplier.supplierName,
                ReceiptItems = r.ReceiptItems
                                    .Select(ReceiptItem => new
                                        {
                                        ReceiptItem.itemId,
                                        ReceiptItem.Item.itemName,
                                        ReceiptItem.itemPackage,
                                        ReceiptItem.itemQnty,
                                        ReceiptItem.itemCost
                                        }
                                    )
            }
        )
       .FirstOrDefaultAsync();

LINQPad的帮助下生成的代码(它是非常好的工具,我建议你们将它用于开发,你可以使用免费版本)

希望如果您遇到这样的挑战,这将有助于您。