c#获取多个List合并、并将相同条件下的值累计sum

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<strudent> one = new List<strudent>();
one.Add(new strudent() { age = , name = "aaa" });
one.Add(new strudent() { age = , name = "bbb" });
one.Add(new strudent() { age = , name = "ddd" });
one.Add(new strudent() { age = , name = "fff" });
one.Add(new strudent() { age = , name = "ggg" });
one.Add(new strudent() { age = , name = "eee" });
List<strudent> two = new List<strudent>();
two.Add(new strudent() { age = , name = "aaa" });
two.Add(new strudent() { age = , name = "bbb" });
two.Add(new strudent() { age = , name = "ddd" });
two.Add(new strudent() { age = , name = "fff" });
two.Add(new strudent() { age = , name = "ggg" });
two.Add(new strudent() { age = , name = "eee" });
two.Add(new strudent() { age = , name = "aaaaa" });
var tt = one.Union(two).ToList();
var res = from p in tt
group p by new { p.name } into g
select new { name = g.Key.name, accp = g.Sum(p => p.age) }; tt.ForEach(x =>
{
Console.WriteLine(x.name + " " + x.age);
});
Console.ReadKey();
}
} public class strudent
{
public decimal age { get; set; }
public string name { get; set; }
}
public class StudentListEquality : IEqualityComparer<strudent>
{
public bool Equals(strudent x, strudent y)
{
return x.name == y.name;
}
public decimal sum(strudent x, strudent y)
{
if (x.name == y.name)
{
return x.age + y.age;
}
return ;
}
public int GetHashCode(strudent obj)
{
if (obj == null)
{
return ;
}
else
{
return obj.ToString().GetHashCode();
}
}
} }
上一篇:Matlab -- Portfolio


下一篇:常见浏览器bug(针对IE6及更低版本)及其修复方法