Linq分组统计元素个数
var g = listreport.GroupBy(x => x.m_fRecentNewPrice);
var results = g.Select(x => new
{
Key = x.Key,
StockCode = x.First().StockCode,
m_fRecentNewPrice = x.First().m_fRecentNewPrice,
Count1 = x.Count(), //here
Count = x.Sum(s => s.m_fRecentNewPrice)
});
foreach (var item1 in results)
{
this._logger.LogInformation($"{actionName} 买一被打掉不停补上:{item1.Key} 总笔数: {item1.StockCode} {item1.m_fRecentNewPrice} {item1.Count1}\n");
}
GroupBy 多属性分组
var groups = data.GroupBy(m => new { m.PropertyA, m.PropertyB})
C# List List<Products>和 List<Categories> 通过ID 用linq组合查询代码
using System;using System.Collections.Generic;using System.Linq;
public class Program
{
public static void Main()
{
List<Categories> categories = new List<Categories>
{
new Categories {
Id =1, CateId =2, CateName = "Cat-B"
},
new Categories { Id =2, CateId =1, CateName = "Cat-A" },
new Categories { Id =3, CateId =1, CateName = "Cat-A" },
}; List<Products> products = new List<Products>
{
new Products { CategoryId = 1, ProductName ="Product B" },
new Products { CategoryId = 2, ProductName ="Product B" },
new Products { CategoryId = 2, ProductName ="Product C" }
};
var query = categories.GroupBy(x => new { x.Id, x.CateId, x.CateName })
.Select(x => new
{
x.Key.Id,
x.Key.CateId,
x.Key.CateName,
ProductsNames = products.Where(p => p.CategoryId == x.Key.CateId)
.Select(p => p.ProductName).ToList()
}).ToList();
foreach (var item in query)
{
Console.WriteLine("Id: {0}, CateId: {1}, CateName: {2}", item.Id, item.CateId, item.CateName);
foreach (var productName in item.ProductsNames)
{
Console.WriteLine(productName);
}
}
}
}
public class Categories
{
public int Id { get; set; }
public int CateId { get; set; }
public string CateName { get; set; }
}
public class Products
{
public int CategoryId { get; set; }
public string ProductName { get; set; }
}
Id: 1, CateId: 2, CateName: Cat - BProduct BProduct CId: 2, CateId: 1, CateName: Cat - AProduct BId: 3, CateId: 1, CateName: Cat - AProduct B.
C# List类型分组求和代码
static void Main(string[] args)
{
List<Person> personList = new List<Person>();
Person p1 = new Person();
p1.Name = "Bob";
p1.Age = "14";
p1.Salary = 1200;
personList.Add(p1);
Person p2 = new Person();
p2.Name = "Bob";
p2.Age = "14";
p2.Salary = 1500;
personList.Add(p2);
Person p3 = new Person();
p3.Name = "Ken";
p3.Age = "56";
p3.Salary = 1000;
personList.Add(p3);
var groupList = personList.GroupBy(m => new { m.Name, m.Age }).
Select(a => new
{
Name = a.Key.Name,
Salary = a.Sum(c=>c.Salary)
}).ToList();
foreach (var item in groupList)
{
Console.WriteLine("Name:{0}--Salary:{1}", item.Name,item.Salary);
}
Console.ReadKey();
}
Linq分组分两种,一种是表分组汇总,另一种是List<>分组汇总。
第一种,表的分组汇总:
为table表赋值
DataTable table = new DataTable();
table.Columns.Add("列名1", Type.GetType("System.Int32"));
table.Columns.Add("列名2", Type.GetType("System.Int32"));
table.Columns.Add("列名3", Type.GetType("System.String"));
table.Columns.Add("列名4", Type.GetType("System.String"));
table.Columns.Add("列名5", Type.GetType("System.String"));
table.Columns.Add("数值或金额列名", Type.GetType("System.String"));
按[列名2]分组汇总
var dtSummary = from q in table.AsEnumerable()
group q by new { 列名2= q.Field<int>("列名2")} into g
select new
{
列名2= g.Key.列名2,
数值或金额汇总列名= g.Sum(a => a.Field<decimal>("数值或金额列名"))
};
第二种,List<>分组汇总:
var listSummary = from p in List<实体>
group p by new { 属性1 = p.属性1 } into g
select new
{
属性1 = g.Key.属性1 ,
数值或金额汇总= g.Sum(a => a.数值或金额属性)
};