2010-05-18 110 views
0

我需要建立一個查詢到實體獲得的記錄,包括:LINQ到實體查詢(1對多)

  • AssetWeapon
  • PersonOut
  • 許多與IsIn = True記錄,
  • 記錄數IsIn = False,
  • 本月的名稱StartTime,

記錄應當由AssetWeapon

alt text http://www.freeimagehosting.net/uploads/4fee01b48c.png

+0

我只是不知道如何建立一個查詢。在db.Transactions_Assets.Include(「交易」)) – Agzam 2010-05-18 14:53:56

+0

好,我得到了這一點,並卡住了... VAR的查詢=從TA 選擇新 { ;我的愚蠢頭腦不正常得到這個工作Weapon = ta.AssetWeapon, Month = ta.Transaction.StartTime.Value.Month, In = ta.IsIn? 1:0, Out = ta.IsIn? 0:1 }; – Agzam 2010-05-18 15:33:46

回答

0

在這裏我得到了什麼最後:

var query = from ta in db.Transactions_Assets.Include("Transaction") 
         let items = new 
         { 
          Weapon = ta.AssetWeapon, 
          Month = ta.Transaction.StartTime.Value.Month, 
          IsIn = ta.IsIn 
         } 
         group items by items.Weapon into g 
         select new { 
          Weapon = g.Key, 
          MonthlyFlow = from m in g 
            group m by m.Month into mg 
            select new { Month = mg.Key, 
               Ins = mg.Count(x => x.IsIn == true), 
               outs = mg.Count(x => x.IsIn == false) 
            } 
         };