Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 1st, 2012  |  syntax: None  |  size: 0.82 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Linq to SQL - Best way to work with a collection of objects inside another object
  2. public class Items
  3. {
  4.     public int ItemID { get; set; }
  5.     public string ItemName { get; set; }
  6.     public List<Bid> Bids { get; set; }
  7. }
  8.  
  9. public class Bids
  10. {
  11.     public int BidID { get; set; }
  12.     public int ItemID  { get; set; }
  13.     public decimal Amount { get; set; }
  14.     public datetime BidTime { get; set; }
  15.     public int CustomerID { get; set; }
  16. }
  17.        
  18. from i in Items
  19. from b in i.Bids
  20. where i.AuctionID == 2 && b.CustomerID == (Int32?)1165
  21. orderby b.BidTime descending
  22. select new
  23. {
  24.     i.ItemID,
  25.     i.ItemName,
  26.     i.Bids
  27. }
  28.        
  29. from i in db.items
  30. where i.AuctionID == 2 && i.Bids.Any(c=>c.CustomerID == (Int32?)1165)
  31. select new
  32. { i.ItemId,
  33.   i.ItemName,
  34.   i.Bids.Where(b=>b.CustomerID == (Int32?)1165 ).OrderbyDescending(b=>b.BidTime)
  35.  }