[SOLVED] How to group grid with one item in the group in c#

Issue

This Content is from Stack Overflow. Question asked by asmgx

I am using c#.net desktop application

I have gridview that looks like this

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    

I want it to be grouped as follows

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

I have almost finished it, but one problem remained.

I want to use the first value of codeScheme and code as shown above

Here is the code that I have done so far.

appreciate any help

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



Solution

I found the answer

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


This Question was asked in StackOverflow by asmgx and Answered by asmgx It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?