伊莉討論區

標題: Linq如何在include中設條件 [打印本頁]

作者: tw00167789    時間: 2017-7-28 08:12 AM     標題: Linq如何在include中設條件

各位大大好:我有二個Model Class
  1. public class Receipt
  2.     {
  3.         [Key]
  4.         [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  5.         public Guid ReceiptId { get; set; }
  6.         public string ReceiptNumber { get; set; }
  7.         public Guid SupplierId { get; set; }
  8.         public DateTime ReceiptDate { get; set; }
  9.         public Guid ReceiptType { get; set; } //進貨, 進退
  10.         [MaxLength(10)]
  11.         public string InvoiceNo { get; set; }   //發票編號
  12.         public int SubTotal { get; set; } //進貨金額
  13.         public Guid TaxType { get; set; }  //稅別
  14.         public int Tax { get; set; }
  15.         public int Amount { get; set; }
  16.         public string ShipAddress { get; set; } //發貨地址, 內定為公司地址
  17.         public int AccountMonth { get; set; }  //結帳月份
  18.         public int Discount { get; set; }   //折讓
  19.         public string Memo { get; set; }
  20.         public bool IsDelete { get; set; }
  21.         public virtual List<ReceiptDetail> ReceiptDetails { get; set; }
  22.         public Supplier Supplier { get; set; }
  23.     }
複製代碼

  1. public class ReceiptDetail
  2.     {
  3.         [Key]
  4.         public Guid Id { get; set; }
  5.         public Guid ReceiptId { get; set; }
  6.         public int ReceiptSeq { get; set; }
  7.         public Guid ProductId { get; set; }
  8.         public int Quantity { get; set; }
  9.         public int UnitPrice { get; set; }
  10.         public int Amount { get; set; }
  11.         public Receipt Receipt { get; set; }
  12.         public Product Product { get; set; }
  13.         public string Memo { get; set; }
  14.         public bool IsDelete { get; set; }
  15.     }
複製代碼
同時有個dbcontext class
  1. public class StockContext : DbContext
  2.     {
  3.         public DbSet<Customer> Customers { get; set; }
  4.         public DbSet<Product> Products { get; set; }
  5.         public DbSet<Supplier> Suppliers { get; set; }
  6.         public DbSet<Receipt> Receipts { get; set; }
  7.         public DbSet<ReceiptDetail> ReceiptDetails { get; set; }
  8.         public DbSet<KeyValue> KeyValues { get; set; }

  9.         protected override void OnModelCreating(DbModelBuilder modelBuilder)
  10.         {
  11.             //進貨單設定複合鍵
  12.             modelBuilder.Entity<ReceiptDetail>().HasKey(k => new {  k.ReceiptSeq, k.Id, });
  13.             //產品(一) --> 進貨明細(多)
  14.             modelBuilder.Entity<Product>()
  15.                             .HasMany(s => s.ReceiptDetails)
  16.                             .WithRequired(r => r.Product);
  17.             //進貨單(一) --> 進貨明細(多)
  18.             modelBuilder.Entity<Receipt>()
  19.                             .HasMany(s => s.ReceiptDetails)
  20.                             .WithRequired(r => r.Receipt);
  21.         }
  22.     }
複製代碼
我想要執行取回一張Recipt及其所關聯的ReceiptDetail, 但ReceiptDetail的IsDelete須為false, 我程式碼如下:
  1. public static Receipt GetFirstByNumberDes()
  2.         {
  3.             using (var context = new StockContext())
  4.             {
  5.                 return context.Receipts.Where(d=>!d.IsDelete).OrderByDescending(d => d.ReceiptNumber)
  6.                     .Include(c=>c.ReceiptDetails.Where(s=>!s.IsDelete))
  7.                     .FirstOrDefault();
  8.             }
  9.         }
複製代碼
但一直產生錯誤, 訊息如下:
The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.
請問可有那位大大知道問題所在? 謝謝
[attach]119681133[/attach]

作者: sam30525sam    時間: 2017-7-28 08:20 PM

本帖最後由 sam30525sam 於 2017-7-29 09:32 PM 編輯

哈囉,麻煩請看以下幾個連結
https://msdn.microsoft.com/zh-tw/library/bb738708(v=vs.110).aspx
https://stackoverflow.com/questions/15980665/ef-lambda-the-include-path-expression-must-refer-to-a-navigation-property

簡單來講就是,
include裡面放的是單一數值,但你放的是一個集合類型的。
你可參考第二個連結修正你的程式。







作者: tw00167789    時間: 2017-7-30 07:24 PM

sam30525sam 發表於 2017-7-28 08:20 PM
哈囉,麻煩請看以下幾個連結
https://msdn.microsoft.com/zh-tw/library/bb738708(v=vs.110).aspx
https:// ...

謝謝回覆, 我找了好久, 可能查詢的方法或關鍵字不對, 沒在stackoverflow中找到, 謝謝你的幫忙




歡迎光臨 伊莉討論區 (http://tw.eyny.com/) Powered by Discuz!