2010-05-18 142 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) 
            } 
         };