LINQ to实体查询(1到多个)

时间:2010-05-18 14:40:41

标签: c# linq entity-framework

我需要为实体构建查询以获取记录,包括:

  • AssetWeapon
  • PersonOut
  • IsIn = True
  • 的记录数量
  • IsIn = False
  • 的记录数量
  • StartTime月份的名称,

记录应按AssetWeapon

分组

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

2 个答案:

答案 0 :(得分:0)

也许这篇文章可以帮到你:

http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx

看投影操作员..

答案 1 :(得分: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)
                                    }
                        };