Please, visit this:
101 LINQ Samples[
^]
Sample:
void Main()
{
List<myobject> objts = new List<myobject>{
new MyObject("a", "a1", "a2", 10),
new MyObject("b", "b1", "a3", 20),
new MyObject("a", "a1", "a2", 30),
new MyObject("a", "b1", "a3", 10)
};
var qry = from o in objts
group o by new{o.Column1, o.Column2, o.Column3} into grp
select new{
Col1 = grp.Key.Column1,
Col2 = grp.Key.Column2,
Col3 = grp.Key.Column3,
AvgCol4 = grp.Average(p=>p.Column4)
};
qry.Dump();
}
class MyObject
{
private string sCol1 = string.Empty;
private string sCol2 = string.Empty;
private string sCol3 = string.Empty;
private int iCol4 = 0;
public MyObject(string _Col1, string _Col2, string _Col3, int _Col4)
{
sCol1 = _Col1;
sCol2 = _Col2;
sCol3 = _Col3;
iCol4 = _Col4;
}
public string Column1
{
get{return sCol1;}
set{sCol1=value;}
}
public string Column2
{
get{return sCol2;}
set{sCol2=value;}
}
public string Column3
{
get{return sCol3;}
set{sCol3=value;}
}
public int Column4
{
get{return iCol4;}
set{iCol4=value;}
}
}
Result:
Col1 Col2 Col3 AvgCol4
a a1 a2 20
b b1 a3 20
a b1 a3 10