linq-to-sql – Linq To SQL Group By和Sum
发布时间:2021-03-30 09:53:39  所属栏目:MsSql  来源:互联网 
            导读:她是一张显示我所拥有的桌子的图片,b我需要显示的网格. opps无法发布图片.我试着解释一下.我的桌子有四个柱子. 项目编号(String) ItemNumber(String) 位置(字符串) 数量.(真实的). 我的网格需要看起来像这样. ProjectNumber ItemNumber QtyMain. 数量其他. 我
                
                
                
            | 
                         她是一张显示我所拥有的桌子的图片,b我需要显示的网格. 
  >项目编号(String) 我的网格需要看起来像这样. > ProjectNumber 我需要写一个linq查询分组evry line所以我将se 1行pr项目/ ItemNumber组合求和数量为2个不同的列1显示到qty其中location是main,1表示qty where location不是(!=)main. linq可以为我做这个,或者怎么办? 解决方法public class Foo
{
    public Int32 ProjectNumber;
    public String ItemNumber;
    public String InventLocation;
    public Int32 Qty;
}
void Main()
{
    List<Foo> foos = new List<Foo>(new[]{
        new Foo { ProjectNumber = 1,ItemNumber = "a",InventLocation = "Main",Qty = 3 },new Foo { ProjectNumber = 1,InventLocation = "Sub",Qty = 2 },Qty = 1 },InventLocation = "Sub2",Qty = 5 }
    });
    var foo = from f in foos
              group f by new { f.ProjectNumber,f.ItemNumber } into fGroup
              select new {
                ProjectNumber = fGroup.Key.ProjectNumber,ItemNumber = fGroup.Key.ItemNumber,QtyMain = fGroup.Where (g => g.InventLocation == "Main").Sum (g => g.Qty),Rest = fGroup.Where (g => g.InventLocation != "Main").Sum (g => g.Qty)
              };
    foo.Dump();
} 
 导致: IEnumerable<> (1 item) ProjectNumber ItemNumber QtyMain Rest 1 a 6 8 (编辑:莱芜站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
相关内容
- sql – Oracle 10g中的Pivot / Crosstab查询(动态列号)
 - sql – 将邻接列表层次结构展平为所有路径的列表
 - sqlserver中在指定数据库的所有表的所有列中搜索给定的值
 - SQL Server中使用sp_password重置SA密码实例
 - 数据库 – 是否有一种优雅的方式来存储双重关系(即用户1和用
 - sqlserver合并DataTable并排除重复数据的通用方法分享
 - sqlserver 统计sql语句大全收藏
 - SQL Server数字开头的数据库表名的解决方法
 - 将数据库数据从Windows上的xampp传输到Ubuntu
 - sqlserver 错误602,未能在sysindexes中找到数据库 的解决办
 
