Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 过滤数据表的最佳方式是什么?_C#_Linq_Datatable - Fatal编程技术网

C# 过滤数据表的最佳方式是什么?

C# 过滤数据表的最佳方式是什么?,c#,linq,datatable,C#,Linq,Datatable,在我的一个c#需求中,我有一个datatable,其中包含以下数据 Category Topics Resourceworked A tp1 Hemant A tp2 Kevin B tp3 Haris B tp4 Hemant B tp5 Hemant C tp6 Kevin 在输出中,我需要两组数据 输出-1:对于每个独特的类别,有多少资源起作用 Category N

在我的一个c#需求中,我有一个datatable,其中包含以下数据

Category Topics Resourceworked 
A        tp1    Hemant
A        tp2    Kevin
B        tp3    Haris
B        tp4    Hemant
B        tp5    Hemant
C        tp6    Kevin
在输出中,我需要两组数据

输出-1:对于每个独特的类别,有多少资源起作用

Category  NoOfResorces
A         2
B         2
C         1
输出-2:resorces为不安静类别(如)工作了多少次

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));

这是您的第一个要求:

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));

是否可以将摘要转换为数据表?我收到一些类型转换错误。是否可以将摘要转换为数据表?我收到一些打字错误。