none
XML to LinQ - GroupBy RRS feed

  • Question

  • Hello,
    I have a one-dimensional list.
    initial list   State = 0 is good,  State = 1 is bad
    // ------------------
    Pos  BoardID  Index  State  ContentID
    1    1        1      1      919000000003001
    2    1        1      0      919000000003001
    3    2        1      0      919000000003001
    4    3        1      0      919000000003001
    5    3        2      0      919000000003002
    6    3        3      0      919000000003003
    7    3        4      0      919000000003004
    8    4        5      1      919000000003005
    
    
    Group by BoardID
    // ------------------
    Pos  BoardID  
    1    1        
    2    2        
    3    3        
    4    4        
    
    Is one of the BoardID bad (state=1) then all are bad
    
    Group by BoardID and with the state
    // ------------------
    Pos  BoardID  State
    1    1          1
    2    2          0   
    3    3          0    
    4    4          1
    
    The goal is to find all Index with a good state and how much ContentID I need.
    
    Group by BoardID and with the good state
    // ------------------
    Pos  BoardID Index  State   ContentID
    1    2       1        0     919000000003001
    2    3       1        0     919000000003001
    3    3       2        0     919000000003002
    4    3       3        0     919000000003003
    5    3       4        0     919000000003004
    How much index I need.
        The result should be in this example 4.  So I need four different ContentIDs
     
    How can I reach this?
    Thanks in advance for your help.


    For better understanding

    structure 0001

    structure 000002 model

    Best regards Markus


    Friday, November 8, 2019 4:31 PM

Answers

  • Second example would be best, similar to this example.

    public class CharacterGroup
    {
        public char Letter { get; set; }
        public int Occurrences { get; set; }
        public int Code { get; set; }
    
        public override string ToString()
        {
            return $"{Letter} {Occurrences} {Code}";
        }
    }

    Then

    var items = "This is a test to see if this really works";
    
    IOrderedEnumerable<CharacterGroup> characterGroup = (items.ToCharArray()
            .GroupBy(chr => chr)
            .Select(grp => new CharacterGroup()
            {
                Letter = grp.Key,
                Occurrences = grp.Count(),
                Code = Convert.ToInt32((int)grp.Key)
            }))
        .ToList()
        .OrderBy((item) => item.Letter);
    
    
    foreach (var item in characterGroup)
    {
        Console.WriteLine(item);
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, November 12, 2019 6:02 PM
    Moderator

All replies

  • Hello,

    Work through the problem in steps, do your groupby then assertions next.

    For grouping by multiple columns see the follow which is simply how to group on more than one property. The state part comes after the groupby else having a where condition will become ugly and hard to maintain.

    https://stackoverflow.com/questions/5231845/c-sharp-linq-group-by-on-multiple-columns


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, November 8, 2019 5:53 PM
    Moderator
  • Hello,


    https://stackoverflow.com/questions/5231845/c-sharp-linq-group-by-on-multiple-columns



    Hello Karen,

    I have a look at it. What I see is the new operator.

    When should I take this? When is this not necessary? Have a nice weekend.

    Greetings Markus

    Saturday, November 9, 2019 12:04 PM
  • Hello,


    https://stackoverflow.com/questions/5231845/c-sharp-linq-group-by-on-multiple-columns



    Hello Karen,

    I have a look at it. What I see is the new operator.

    When should I take this? When is this not necessary? Have a nice weekend.

    Greetings Markus

    When you have multiple keys in a group-by for the new keyword.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, November 9, 2019 12:47 PM
    Moderator
  • Hello Karen,
    var qryPIndex = hsProduct.Where(s => s.Item1 == 1).GroupBy(info => info.Item3)
    .Select(group => new
    {
    	PIndex = group.Key,
    	Occurrences = group.Count()
    })
    .OrderBy(x => x.PIndex).ToList();
    Basically, it works. I create multiple queries because I think it's more transparent. Step by step as my description. Thanks.
    // Second case 
    var qryPIndex = hsProduct.Where(s => s.Item1 == 1).GroupBy(info => info.Item3)
    .Select(group => new  IndexOccurrences
    {
    	PIndex = group.Key,
    	Occurrences = group.Count()
    })
    .OrderBy(x => x.PIndex).ToList();
    I can also specify a concrete class after the 'new' operator. Then it must exist. Do I see that correctly?
    With best regards Markus



    Tuesday, November 12, 2019 5:26 PM
  • Second example would be best, similar to this example.

    public class CharacterGroup
    {
        public char Letter { get; set; }
        public int Occurrences { get; set; }
        public int Code { get; set; }
    
        public override string ToString()
        {
            return $"{Letter} {Occurrences} {Code}";
        }
    }

    Then

    var items = "This is a test to see if this really works";
    
    IOrderedEnumerable<CharacterGroup> characterGroup = (items.ToCharArray()
            .GroupBy(chr => chr)
            .Select(grp => new CharacterGroup()
            {
                Letter = grp.Key,
                Occurrences = grp.Count(),
                Code = Convert.ToInt32((int)grp.Key)
            }))
        .ToList()
        .OrderBy((item) => item.Letter);
    
    
    foreach (var item in characterGroup)
    {
        Console.WriteLine(item);
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, November 12, 2019 6:02 PM
    Moderator