public class Test : IEnumerable<Test>
{
public string ColName
{
get;
set;
}
public int ColVal
{
get;
set;
}
#region IEnumerable<Test> Members
public IEnumerator<Test> GetEnumerator()
{
throw new NotImplementedException();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
#endregion
}
{
public string ColName
{
get;
set;
}
public int ColVal
{
get;
set;
}
#region IEnumerable<Test> Members
public IEnumerator<Test> GetEnumerator()
{
throw new NotImplementedException();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
#endregion
}
Code to execute:
List<Test> tests = new List<Test>();
Test test = new Test();
test.ColName = "A";
test.ColVal = 123;
tests.Add(test);
test = new Test();
test.ColName = "A";
test.ColVal = 10;
tests.Add(test);
test = new Test();
test.ColName = "B";
test.ColVal = 231;
tests.Add(test);
var query1 = from t in tests
group t by t.ColName into gr
select new
{
Id = gr.Key,
Sum = gr.Sum(r => r.ColVal)
};
foreach (var grp in query1)
{
Console.WriteLine("{0}\t{1}", grp.Id, grp.Sum);
}
Test test = new Test();
test.ColName = "A";
test.ColVal = 123;
tests.Add(test);
test = new Test();
test.ColName = "A";
test.ColVal = 10;
tests.Add(test);
test = new Test();
test.ColName = "B";
test.ColVal = 231;
tests.Add(test);
var query1 = from t in tests
group t by t.ColName into gr
select new
{
Id = gr.Key,
Sum = gr.Sum(r => r.ColVal)
};
foreach (var grp in query1)
{
Console.WriteLine("{0}\t{1}", grp.Id, grp.Sum);
}