LINQ group

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Morris

    LINQ group

    responseData.Si gnals is a List<SignalInfo >

    var signalGroups =
    from signal in responseData.Si gnals
    where signal.DisplayP ath.StartsWith( "AppMenu")
    orderby signal.DisplayP ath
    group signal by signal.DisplayP ath into signalGroup
    select new { Title = signalGroup.Key , Signals = signalGroup };

    foreach(var signalGroup in signalGroups)
    foreach(SignalI nfo signal in ?????????????

    How do I get each SignalInfo in the group?


    Thanks

    Pete
  • Peter Morris

    #2
    Re: LINQ group

    This seems very innefficient so there must be a better way of getting a list
    of SignalInfo by DisplayPath.

    var signalGroups =
    from signal in responseData.Si gnals
    where signal.DisplayP ath.StartsWith( "AppMenu")
    orderby signal.DisplayP ath
    group signal by signal.DisplayP ath into signalGroup
    select new
    {
    Title = signalGroup.Key .Split('/')[2],
    Signals = new List<SignalInfo >(from signal in responseData.Si gnals
    where signal.DisplayP ath == signalGroup.Key select signal)
    };


    Pete

    Comment

    • Martin Honnen

      #3
      Re: LINQ group

      Peter Morris wrote:
      responseData.Si gnals is a List<SignalInfo >
      >
      var signalGroups =
      from signal in responseData.Si gnals
      where signal.DisplayP ath.StartsWith( "AppMenu")
      orderby signal.DisplayP ath
      group signal by signal.DisplayP ath into signalGroup
      select new { Title = signalGroup.Key , Signals = signalGroup };
      >
      foreach(var signalGroup in signalGroups)
      foreach(SignalI nfo signal in ?????????????
      Try
      foreach(SignalI nfo signal in signalGroup.Sig nals)


      --

      Martin Honnen --- MVP XML

      Comment

      • Frans Bouma [C# MVP]

        #4
        Re: LINQ group

        Peter Morris wrote:
        responseData.Si gnals is a List<SignalInfo >
        >
        var signalGroups =
        from signal in responseData.Si gnals
        where signal.DisplayP ath.StartsWith( "AppMenu")
        orderby signal.DisplayP ath
        group signal by signal.DisplayP ath into signalGroup
        select new { Title = signalGroup.Key , Signals = signalGroup };
        >
        foreach(var signalGroup in signalGroups)
        foreach(SignalI nfo signal in ?????????????
        >
        How do I get each SignalInfo in the group?
        As Martin said in the other post, simply iterate over teh Signals property.

        As you return a new type with just the key and the group, you can also do:
        var signalGroups =
        from signal in responseData.Si gnals
        where signal.DisplayP ath.StartsWith( "AppMenu")
        orderby signal.DisplayP ath
        group signal by signal.DisplayP ath into signalGroup
        select signalGroup;

        You can then foreach over the query as:
        foreach(IGroupi ng<string, Signalgroup in signalGroups)
        {
        // now obtain each signal in the current group
        foreach(Signal s in group)
        {
        }
        // the key is obtainable by group.Key
        }

        FB

        --
        ------------------------------------------------------------------------
        Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
        LLBLGen Pro website: http://www.llblgen.com
        My .NET blog: http://weblogs.asp.net/fbouma
        Microsoft MVP (C#)
        ------------------------------------------------------------------------

        Comment

        • Peter Morris

          #5
          Re: LINQ group

          foreach(IGroupi ng<string, Signalgroup in signalGroups)
          foreach(Signal s in group)
          That was exactly the info I needed, thanks very much!



          Pete

          Comment

          Working...