Linq对DataTable或者集合的排序,Where筛选,分组,统计总数sum等操作

 

//对集合进行筛选,排序,分组

var query = (from x in dsResult.Tables[0].AsEnumerable()
                Where DataTrans.CBoolean(x["IsChecked"]) == true
        Group x By new
        {
            no = x.Field<string>("NO"),
            ptno = x.Field<string>("PTNO"),
            ver = x.Field<int>("VER"),
            kd = x.Field<string>("KD"),
            que_da = Convert.ToDateTime(x.Field<DateTime>("QUE_DA").ToString("yyyy/MM/dd"))
        } into g
        OrderBy g.Key.no,g.Key.ptno,g.Key.ver,g.Key.kd,g.Key.que_da
        select new
        {
            qty = g.Sum(x => Convert.ToInt32(x["QUE_QTY"])),
            stock=g.Sum(x=>Convert.ToInt32(x["STOCK"])),
            no=g.Key.no ,
            ptno=g.Key.ptno,
            ver=g.Key.ver,
            kd=g.Key.kd,
            que_da=g.Key.que_da
        });
//统计一列的总数


var dtUnCheckNum = objFinanceCheckDB.UnCheckNum_Get(strReportDate1);
var queryTotal = (from DataRow row in dtUnCheckNum.Rows
                  select new { qsum = row["UnCheckNum"] }).Sum(a => Convert.ToInt32(a.qsum));
if (queryTotal == 0)
{
   lblWrongNum.Text = "0";
}
//简单按条件过滤

var records = dt.AsEnumerable().Where(a => Convert.ToInt32(a[0]) < 1000);
DataView rsl = records.AsDataView();

你可能感兴趣的:(query,LINQ)