linq 分组

var data = from r in listRecords
                       group r by
                               new
                               {
                                   r.CampaignId,
                                   r.CityId,
                                   r.Gift_DistributorId,
                                   r.ProductGiftId,

                                   FirstQuantity = listRecords.Where(a => a.CampaignId == r.CampaignId
                                       && a.CityId == r.CityId
                                       && a.Gift_DistributorId == r.Gift_DistributorId
                                       && a.ProductGiftId == r.ProductGiftId).Sum(c => c.FirstQuantity),

                                   Premium = listRecords.Where(a => a.CampaignId == r.CampaignId
                                                            && a.CityId == r.CityId
                                                            && a.Gift_DistributorId == r.Gift_DistributorId
                                                            && a.ProductGiftId == r.ProductGiftId).Sum(c => c.Premium)
                               } into g
                       select g.Key
                          ;参考  http://stackoverflow.com/questions/5231845/c-sharp-linq-group-by-on-multiple-column

//修改后
var data2 = from r in listRecords
                       group r by
                               new
                               {
                                   r.CampaignId,
                                   r.CityId,
                                   r.Gift_DistributorId,
                                   r.ProductGiftId,

                               } into g
                       select new
                       {
                           g.Key.CampaignId,
                           g.Key.CityId,
                           g.Key.Gift_DistributorId,
                           g.Key.ProductGiftId,
                           FirstQuantity = g.Sum(a => a.FirstQuantity),
                           Premium = g.Sum(a => a.Premium)
                       };
时间: 2024-10-17 11:02:04

linq 分组的相关文章

Linq分组功能

Linq在集合操作上很方便,很多语法都借鉴自sql,但linq的分组却与sql有一定的区别,故整理发布如下. 1.  Linq分组 分组后以Key属性访问分组键值. 每一组为一个IEnumberAble或IQeuryAble的集合,可以继续枚举. Sample: string[] World = { "Hello","World"}; string[] Brother = { "Hello","Brother"}; var r

Linq分组合并

有一个List,要求按日期分组,将同一天的数据组合起来,用Linq实现. namespace ConsoleApplication1 { class Program { static void Main(string[] args) { List<Student> students = new List<Student>() { new Student(){ ID = 1 , Name ="guwei1", BirthDay=DateTime.Now.AddHo

Linq分组,linq方法分组

Group在SQL经常使用,通常是对一个字段或者多个字段分组,求其总和,均值等. Linq中的Groupby方法也有这种功能.具体实现看代码: 假设有如下的一个数据集: view sourceprint? 01.public class StudentScore 02.{ 03.public int ID { set; get; } 04.public string Name { set; get; } 05.public string Course { set; get; } 06.publi

Linq 分组(group by)求和(sum)并且按照分隔符(join)分割列数据

转载:http://www.cnblogs.com/zq281660880/archive/2012/09/26/2704836.html 今天在使用linq处理一下需求时碰到一点小问题,特此记录. 需求: 按照品名相同的进行汇总,数量相加.表号按分号分割显示 1.组织测试数据表 DataTable tableA1 = new DataTable(); tableA1.Columns.AddRange(new DataColumn[] { new DataColumn("品名"), n

linq 分组包含时间操作

EntityFunctions.TruncateTime linq 时间转化操作 var _date = DateTime.Now.Date; var q = from p in AdDividend.ObjectSet() where EntityFunctions.TruncateTime(p.SettlementDate) == _date group p by p.UserId into g select new { g.Key, DayMaxGold = g.Sum(p => p.Go

Linq分组

1.lin语句 int[] nums = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0, 3 }; DataTable table = new DataTable("Numbers"); table.Columns.Add("number", typeof(int)); foreach (int n in nums) { table.Rows.Add(new object[] { n }); } var numbers = table.AsEnume

linq分组查询

string[] arrStr = { ".com", "www.baidu.com", "www.qq.com", "www.bing.cn", "www.avc.net","www.vvv.cn","www.bbb.net" }; //定义查询规则 var addressG = from gg in arrStr                          

DataTable 用linq分组查询

DataRow drt = null; var tlist = dt.Select("Atmbs LIKE '%" + d["Two_Code"] + "%'").AsEnumerable().GroupBy(a => a.Field<string>("STATUS_MISSIONN")).Select(e => new { STATUS_MISSIONN = e.Key, Number = e.Cou

C# 使用Dictionary、linq实现根据集合里面的字符串进行分组

分组两种方法: List<string> list = new List<string>() { "1_32", "2_10", "1_8", "1_25", "2_3", "3_5", "5_15", "3_16" }; //对上面集合里面的字符串按照“_”进行分组. #region //使用字典,键值对集合保存分组数据.