Click here to Skip to main content
15,911,030 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Having a datatable with 4 columns, sequence of 3 columns is duplicated with 4th column value differs. The resultant table has the average value in fourth column of duplicate rows and the sequence of first 3 columns as unique of input table.

Column1 Column2 Column3 Column4
a a1 a2 10
b b1 a3 20
a a1 a2 30
a b1 a3 10

Resultant table as:
Column1 Column2 Column3 Column4
a a1 a2 20
b b1 a3 20
a b1 a3 10

Please let us know if u require any further info

Please suggest.
Posted
Updated 17-Dec-14 2:09am
v3
Comments
Tomas Takac 17-Dec-14 5:36am    
I don't quite understand. Could you add some sample data? Your inputs and expected outputs? And maybe post your query to show what you have tried so far.
julakantiharshith 17-Dec-14 5:41am    
Column1 Column2 Column3 Column4
a a1 a2 10
b b1 a3 20
a a1 a2 30
a b1 a3 10

Resultant table as:
Column1 Column2 Column3 Column4
a a1 a2 20
b b1 a3 20
a b1 a3 10

Please let us know if u require any further info
Tomas Takac 17-Dec-14 5:48am    
Use "improve question" button to add details to your question.
Tomas Takac 17-Dec-14 5:51am    
You want to do this in C#? How do you store the data?
KM Perumal 17-Dec-14 6:53am    
@julakantiharshith try below solution

Please, visit this: 101 LINQ Samples[^]

Sample:
C#
//using LiqPad4
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();
}

// Define other methods and classes here
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
 
Share this answer
 
v3
try this
C#
datatable = datatable .AsEnumerable()
       .GroupBy(r =>  r["Column4"])
       .CopyToDataTable();
 
Share this answer
 
Comments
Maciej Los 17-Dec-14 8:12am    
And where is average?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900