linq 如何在C#中将网格与组中的一个项分组

h4cxqtbf  于 2022-12-06  发布在  C#
关注(0)|答案(1)|浏览(197)

我正在使用c#.net桌面应用程序
网格视图如下所示

ibengin   iend   code    preferredText     Affirmation    tag     codeScheme    Value
1         10             Kitkat            Yes            Choc    
11        15             Mars              Yes            Choc    
16        20             Bounty            Yes            Choc    
21        27     A1      Kitkat            Yes            Choc    USA
28        32             Bounty            Yes            Choc    
33        47             Bounty            No             Choc    
48        61     A1      Kitkat            Yes            Choc    USA
62        65     B7      Mars              Yes            Choc    UK
66        77             Kitkat            Yes            Choc    USA
78        81             Kitkat            Yes            Choc

我希望将其分组如下

Affirmation   PreferredText   Count    Value   CodingScheme   Code    Positions
Yes           Kitkat          5                USA            A1     1:10,21:27,48:61,66:77,78:81
Yes           Mars            2                UK             B7     11:15,62:65
Yes           Bounty          2                                      16:20,28:32
No            Bounty          1                                      33:47

我几乎完成了,但还有一个问题。
我想使用codeScheme的第一个值和如上所示的代码
下面是我目前为止完成的代码。
感谢任何帮助

var lstInfo = grdBreakDown.Rows.Cast<DataGridViewRow>()
                               .Where(x => !x.IsNewRow)                   // either..
                               .Where(x => x.Cells["Tag"].Value.ToString() == Tag) //..or or both
                               .GroupBy(x => new
                               {
                                   grpAffirmation = x.Cells["Affirmation"].Value.ToString(),
                                   grpPreferredText = x.Cells["preferredText"].Value.ToString(),
                                   grpValue = (cbxValue.Checked ? x.Cells["Value"].Value.ToString() : "")
                               })
                               .Select(y => new
                               {
                                   Affirmation = y.Key.grpAffirmation,
                                   PreferredText = y.Key.grpPreferredText,
                                   Count = y.Count(),//.ToString(),
                                   Value = y.Key.grpValue,
                                   Positions = string.Join(",", y.Select(i => i.Cells["ibegin"].Value.ToString() + ":" + i.Cells["iend"].Value.ToString()))
                               })
                               .OrderByDescending(y => y.Count)
                               .ToList();
dgtucam1

dgtucam11#

我找到了答案

var lstInfo = grdBreakDown.Rows.Cast<DataGridViewRow>()
                               .Where(x => !x.IsNewRow)                   // either..
                               .Where(x => x.Cells["Tag"].Value.ToString() == Tag) //..or or both
                               .GroupBy(x => new
                               {
                                   grpAffirmation = x.Cells["Affirmation"].Value.ToString(),
                                   //grpCodingScheme = x.Cells["codingScheme"].Value.ToString(),
                                   //grpCode = x.Cells["code"].Value.ToString(),
                                   grpPreferredText = x.Cells["preferredText"].Value.ToString(),
                                   grpValue = (cbxValue.Checked ? x.Cells["Value"].Value.ToString() : "")
                               })
                               .Select(y => new
                               {
                                   Affirmation = y.Key.grpAffirmation,
                                   CodingScheme = string.Join(",", y.Select(i => i.Cells["codingScheme"].Value.ToString()).Distinct()).TrimEnd(','),
                                   Code = string.Join(",", y.Select(i => i.Cells["code"].Value.ToString()).Distinct()).TrimEnd(','),
                                   PreferredText = y.Key.grpPreferredText,
                                   Count = y.Count(),//.ToString(),
                                   Value = y.Key.grpValue,
                                   Positions = string.Join(",", y.Select(i => i.Cells["ibegin"].Value.ToString() + ":" + i.Cells["iend"].Value.ToString()))
                               })
                               .OrderByDescending(y => y.Count)
                               .ToList();

相关问题