日期:2014-05-20  浏览次数:20767 次

# Linq To DataTable应用示例
Linq to DataTable范例代码
       以下代码段经测试通过,发现C# Linq其实还是挺好用的。只不过感觉语法上有点怪异, 如果格式上同标准SQL语法一致,这样程序员可能更好地掌握强大的Linq的
       private void btnLinqTest__Click(object sender, EventArgs e)
        {
            DataTable vtblSeqBase = this.DM.bdsSingle._mpDataTable;
            //1. GroupBy 统计Count      XXXTestOk 2011-07-18
            var query =
               from q in vtblSeqBase.AsEnumerable()
               group q by q.Field<string>("MachineType") into r
               select new
               {
                   _qMachType = r.Key,
                   _qCount = r.Count()
               };
            using (DataTable vtblCount = new DataTable())
            {
                DataRow vNewRow = null;
                vtblCount.Columns.Add("MachType", typeof(string));
                vtblCount.Columns.Add("Count", typeof(int));
                foreach (var vq in query)
                {
                    vNewRow = vtblCount.NewRow();
                    vNewRow["MachType"] = vq._qMachType;
                    vNewRow["Count"] = vq._qCount;
                    vtblCount.Rows.Add(vNewRow);
                }
                //vtblCount.mmViewForRD();
            }
            //2. Distinct用法       XXXTestOk 2011-07-18
            var d = (from DataRow vRow in vtblSeqBase.Rows
                     select new { _MachType = vRow["MachineType"] }).Distinct();
            using (DataTable vtblDistinct = new DataTable())
            {
                DataRow vNewRow = null;
                vtblDistinct.Columns.Add("MachType", typeof(string));
                vtblDistinct.Columns.Add("Count", typeof(int));
                foreach (var q in d)
                {
                    vNewRow = vtblDistinct.NewRow();
                    vNewRow["MachType"] = q._MachType;
                    vtblDistinct.Rows.Add(vNewRow);
                }
                //vtblDistinct.mmViewForRD();
            }
            //DataTableExtensions.CopyToDataTable
            //3.
            List<int> vListNum = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };
            IEnumerable<int> vDistNum = vListNum.Distinct();
            foreach (int n in vDistNum)
            {
                //MessageBox.Show("不重复的数值 =" + n.ToString());
            }
            //4. Group By 一个字段
            var querySum =
                from q in vtblSeqBase.AsEnumerable()
                group q by q.Field<string>("MachineType") into g
                select new
                {
                    _qMachType = g.Key,
                    _qSamTotal = g.Sum(q => q.Field<decimal>("SamValue"))
                };
            using (DataTable vtblSAMTotal  = new DataTable())
            {
                DataRow vNewRow = null;
                vtblSAMTotal.Columns.Add("MachType", typeof(string));
                vtblSAMTotal.Columns.Add("SAMTotal", typeof(decimal));
                foreach (var vq in querySum)
                {
                    vNewRow = vtblSAMTotal.NewRow();
                    vNewRow["MachType"] = vq._qMachType;
                    vNewRow["SAMTotal"] = vq._qSamTotal;
                    vtblSAMTotal.Rows.Add(vNewRow);
                }
                //vtblSAMTotal.mmViewForRD();
            }
            //5. Group By 多个字段 GBN = Group By NField
            var queryGBN  =
                from q in vtblSeqBase.AsEnumerable()
                group q by new {机器代码=q.Field<string>("MachineType"), 工序代码=q.Field<string>("SeqCode") }
                into g
                select new
                {
                    _qMachType = g.Key,
                    _qSamTotal = g.Sum(q => q.Field<decimal>("SamValue")),
                    _qSamAvg = g.Average(q => q.Field<decimal>("SamValue"))
                };
            using (DataTable vtblSAMTotal = new DataTable())
            {
                DataRow vNewRow = null;
                vtblSAMTotal.Columns.Add("MachType", typeof(string));
                vtblSAMTotal.Columns.Add("SeqCode", typeof(string));
                vtblSAMTotal.Columns.Add("SAMTotal", typeof(decimal));
                vtblSAMTotal.Columns.Add("SAMAvg", typeof(decimal));
                foreach (var vq in queryGBN)
                {
                    vNewRow = vtblSAMTotal.NewRow();
                    vNewRow["MachType"] = vq._qMachType;
                    vNewRow["SeqCode"] = vq._qMachType;
                    vNewRow["SAMTotal"] = vq._qSamTotal;
                    vNewRow["SAMAvg"] = vq._qSamAvg;
                    vtblSAMTotal.Rows.Add(vNewRow);
                }
                vtblSAMTotal.mmViewForRD();
            }
        }