簡體   English   中英

篩選數據表的最佳方法是什么?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM