C# 匿名类型 分组 求和
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication8 { class pr { public int Id { get; set; } public string Name { get; set; } public string Name2 { get; set; } } class Program { static void Main(string[] args) { List<pr> prarr = new List<pr> { new pr() { Id = 1, Name = "A" }, new pr() { Id = 2, Name = "B" }, new pr() { Id = 3, Name = "C" }, new pr() { Id = 4, Name = "D" }, new pr() { Id = 4, Name = "D" }, }; var lista_ = prarr.Select(a => new { a.Id, a.Name }); var sdsds = lista_.GroupBy(a => a.Name).Select(G => new { G.Key, ID = G.Sum(item => item.Id) }); Console.WriteLine(); //var list1 = new { Id = 1, Name = "asd1" }; //var list2 = new { Id = 2, Name = "asd2" }; //var list3 = new { Id = 3, Name = "asd3" }; //var list4 = new { Id = 4, Name = "asd4" }; //var list5 = new { Id = 4, Name = "asd4" }; //var list = new {new // { Id = 1, Name = "asd1" } , // new { Id = 2, Name = "asd2" }, // new { Id = 3, Name = "asd3" }, // new { Id = 4, Name = "asd4" }, // new { Id = 4, Name = "asd4" } // }; } } }