標籤:www c# err color string erro 不重複 entry 類型
https://www.cnblogs.com/johnblogs/p/6006867.html
DataTable ds = new DataTable();
//1、lamda 運算式寫法(推薦)
var result = ds.AsEnumerable().GroupBy(s => new{Year = s.Field<int>("Year"), Month = s.Field<int>("Month"), Day = s.Field<int>("Day")});
//2、Linq寫法 最終編譯器會把它轉化為lamda運算式
//var result = from s in ds.Tables[0].AsEnumerable()
group s by new { Year = s.Field<int>("Year"), Month = s.Field<int>("Month"), Day = s.Field<int>("Day") } into temp
select temp;
//DataTable 使用GroupBy方法需要注意result為IGrouping<int,DataRow>類型
foreach (var thisGroup in result)
{
foreach (var row in thisGroup)
{
//遍曆當前這組的所有row
}
}
//統計不重複的數量,沒測試不知是否能用dt.DefaultView.ToTable(true, new string[1] { "UserName" }).Rows.Count; http://www.xuebuyuan.com/1990057.htmlusing (DataTable dt = ds.Tables[0])
{
//三個變數分別記錄總記錄數、不重複的使用者數、所有使用者的金額總額
int rowsCount, distinctUserRowsCount, AllUserMoney;
rowsCount = dt.Rows.Count;
distinctUserRowsCount = dt.DefaultView.ToTable(true, new string[1] { "UserName" }).Rows.Count;
AllUserMoney = Convert.ToDecimal(dt.Compute("sum(UserMoney)", ""));
}
C#DataTable 使用GroupBy方法的lamda 運算式和Linq語句寫法