在数据表中平均重复的更好方法 [英] Better way for average of duplicates in datatable

查看:58
本文介绍了在数据表中平均重复的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有4列的数据表,3列的序列重复,第4列值不同。结果表具有第四列重复行的平均值和前3列的序列作为输入表的唯一值。



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



结果表格如下:

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



如果您需要任何进一步的信息,请告诉我们



请建议。

解决方案

请访问: 101 LINQ示例 [ ^ ]



示例:

  //  使用LiqPad4  
void Main()
{
List< myobject> objts = new 列表< myobject> {
new MyObject( a a1 a2 10 ),
new MyObject( b b1 a3 20 ),
new MyObject( a a1 a2 30 ),
new MyObject( a,< span class =code-string> b1 a3 10
};

var qry = 来自 o in 通过 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 ;}
}
}



结果:

<前郎=文字> Col1 Col2 Col3 AvgCol4
a a1 a2 20
b b1 a3 20
a b1 a3 10


试试这个

 datatable = datatable .AsEnumerable()
.GroupBy(r = > r [ Column4 ])
.CopyToDataTable();


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.

解决方案

Please, visit this: 101 LINQ Samples[^]

Sample:

//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


try this

datatable = datatable .AsEnumerable()
       .GroupBy(r =>  r["Column4"])
       .CopyToDataTable();


这篇关于在数据表中平均重复的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆