使用linq进行排序 条件判断 分组求和 希望高手写个简单的例子
使用linq进行排序 条件判断 分组求和(三个条件都需要) 希望高手写个简单的例子 我是新手 请大家多多帮助 谢谢
------解决方案--------------------public class Test
{
public string Name{get;set;}
public int Num{ get; set; }
}
protected void Click(object sender, EventArgs e)
{
List<Test> list = new List<Test>();
list.Add(new TestData() { Name = "A", Num=50});
list.Add(new TestData() { Name = "A", Num=50 });
list.Add(new TestData() { Name = "A",Num=50 });
list.Add(new TestData() { Name = "B",Num=50});
list.Add(new TestData() { Name = "C", Num=50 });
list.Add(new TestData() { Name = "C", Num=50 });
var query = from l in list
group l by new { l.Name, l.Num} into g
select new
{
Name = g.Key.Name,
Num= g.Sum(a => a.Num)
};
string result = "";
foreach (var q in query)
{
}
}
------解决方案--------------------
void Main()
{
List<Test> list = new List<Test>();
list.Add(new TestData() { Name = "A", Num=50});
list.Add(new TestData() { Name = "A", Num=50 });
list.Add(new TestData() { Name = "A",Num=70 });
list.Add(new TestData() { Name = "B",Num=50});
list.Add(new TestData() { Name = "C", Num=50 });
list.Add(new TestData() { Name = "C", Num=50 });
var query = from l in list
where l.Num==50 //条件判断