C
C#•9mo ago
Mario K.

How to perform nested one-to-many join in LINQ

I apologize if this is stupid, but how to return an IEnumerable<Receipt> that contains Product in this case: // Each Receipt can have many ReceiptDetals public class Receipt { public int Id { get; set; } public virtual ICollection<ReceiptDetail> ReceiptDetails { get; set; } = new List<ReceiptDetail>(); } // Each ReceiptDetail is related to one Product public class ReceiptDetail { public int Id { get; set; } // Other properties .... public int ProductId { get; set; }
public Product Product { get; set; } = new Product(); } // A Product can be present in none or many ReceiptDetails !!! public class Product { public int Id { get; set;}
// Other properties ... public virtual ICollection<ReceiptDetail> ReceiptDetails { get; set; } = new List<ReceiptDetail>(); } context.Receipt.Include(m => m.ReceiptDetail) returns a list of Receipt, with included ReceiptDetails, but Product within ReceiptDetails is NULL ! Also, context.Receipt.Include(m => m.ReceiptDetal).Include(x => x.Product) does not work, and neither does context.Receipt.Include(m => m.ReceiptDetal.Product) ... What am I missing ? Any suggestions or help is appreciated !
8 Replies
SpReeD
SpReeD•9mo ago
$codegif
Kringe
Kringe•9mo ago
context.Receipt.Include(m => m.ReceiptDetail).ThenInclude(rd => rd.Product)
context.Receipt.Include(m => m.ReceiptDetail).ThenInclude(rd => rd.Product)
Mario K.
Mario K.OP•9mo ago
ThenInclude !!!!!
Kringe
Kringe•9mo ago
haha yes
Mario K.
Mario K.OP•9mo ago
Yeeeee Thats the problem with linq... Trying to be sooooo human friendly They should make keyword "please" and "thanks" to begin and end a query 🙂
Kringe
Kringe•9mo ago
I love linq i wouldn't mind adding a thanks after a query
Mario K.
Mario K.OP•9mo ago
Lol ! Please from q in table2 select x and thank you so much 😉
Want results from more Discord servers?
Add your server