[英]What is best way of filter datatable?
在我的C#要求之一中,我有数据表,其中有以下数据
Category Topics Resourceworked
A tp1 Hemant
A tp2 Kevin
B tp3 Haris
B tp4 Hemant
B tp5 Hemant
C tp6 Kevin
在输出中,我需要两组数据
OutPut-1:对于每个唯一类别,有多少资源起作用
Category NoOfResorces
A 2
B 2
C 1
输出2:对像这样的非正常类别,resoces工作了多少次
Category Resource NoOfTime
A Hemant 1
A Kevin 1
B Haris 1
B Hemant 2
C Kevin 1
实现输出的最佳方法是什么,即数据表过滤器或LINQ?
另外:LINQ的任何人都可以告诉我良好的在线网站或书籍以学习LINQ吗?
这是您的第一个要求:
var uniqueCat = from d in tblData.AsEnumerable()
group d by (string)d["Category"] into Group
select Group;
var catRes = from grp in uniqueCat
let c = grp.Select(r => r["Resourceworked"]).Distinct().Count()
select new {Category = grp.Key, NoOfResorces=c};
var summary = from cr in catRes
select string.Format("Category:{0} Count:{1}",cr.Category,cr.NoOfResorces);
MessageBox.Show(string.Join(Environment.NewLine,summary));
这是第二个查询:
var uniqueCatRes = from d in tblData.AsEnumerable()
group d by new{Cat= d["Category"], Res=d["Resourceworked"]} into Group
select Group;
var catResCount = from grp in uniqueCatRes
let Category = grp.Key.Cat
let Resource = grp.Key.Res
let NoOfResorces = grp.Count()
select new { Category,Resource,NoOfResorces };
summary = from crc in catResCount
select string.Format("Category:{0} Resource:{1} Count:{2}", crc.Category,crc.Resource, crc.NoOfResorces);
MessageBox.Show(string.Join(Environment.NewLine,summary));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.