locked
LINQ Group and Count RRS feed

  • Question

  • I have the following, at the moment I retrieve the reader with the strongest signal, what I want to do is exclude any reader which does not have 4 entries in the memory

       results = From t In m_lstTagLocationMemory
            Group t By t.ReaderID Into byReader = Group
            Order By byReader.Average(Function(x) x.SignalStrength) Descending
            Select New StrongestReader() With
                {
                   .ReaderID = ReaderID,
                   .AverageSignalStrength = byReader.Average(Function(x) x.SignalStrength),
                   .Occurences = byReader.Count(),
                   .LastPacketRecieved = byReader.Max(Function(x) x.PacketDate)
                 }
      
        Public Class TagLocationMemory
    
        Private m_strReaderID As String
        Private m_dSignalStrength As Double
        Private m_dtPacketDate As Date
    
        Public ReadOnly Property ReaderID As String
            Get
                Return m_strReaderID
            End Get
        End Property
    
        Public ReadOnly Property SignalStrength As Double
            Get
                Return m_dSignalStrength
            End Get
        End Property
    
        Public ReadOnly Property PacketDate As Date
            Get
                Return m_dtPacketDate
            End Get
        End Property
     

    <style type="”text/css”">.identifier {color:black !important;}</style>
    • Moved by CoolDadTx Thursday, July 6, 2017 12:59 AM VB related
    Tuesday, July 4, 2017 2:31 PM

Answers

  • Hi Darren,

    You should be able to insert a WHERE clause just before the select:

    Dim results = From t In m_lstTagLocationMemory
                  Group t By t.ReaderID Into byReader = Group
                  Order By byReader.Average(Function(x) x.SignalStrength) Descending
                  Where byReader.Count > 3
                  Select New StrongestReader() With
                  {
                     .ReaderID = ReaderID,
                     .AverageSignalStrength = byReader.Average(Function(x) x.SignalStrength),
                     .Occurances = byReader.Count(),
                     .LastPacketReceived = byReader.Max(Function(x) x.PacketDate)
                   }
    

    I tested with some junk data as follows and reader "r3" is excluded from the results:

    Dim m_lstTagLocationMemory As New List(Of TagLocationMemory)
    With m_lstTagLocationMemory
        .Add(New TagLocationMemory("r1", 2.5, Now))
        .Add(New TagLocationMemory("r1", 3.5, Now))
        .Add(New TagLocationMemory("r1", 4.5, Now))
        .Add(New TagLocationMemory("r1", 5.5, Now))
    
        .Add(New TagLocationMemory("r2", 2.5, Now))
        .Add(New TagLocationMemory("r2", 3.5, Now))
        .Add(New TagLocationMemory("r2", 4.5, Now))
        .Add(New TagLocationMemory("r2", 1.5, Now))
    
        .Add(New TagLocationMemory("r3", 2.5, Now))
        .Add(New TagLocationMemory("r3", 3.5, Now))
        .Add(New TagLocationMemory("r3", 4.5, Now))
    
        .Add(New TagLocationMemory("r4", 5.5, Now))
        .Add(New TagLocationMemory("r4", 3.5, Now))
        .Add(New TagLocationMemory("r4", 4.5, Now))
        .Add(New TagLocationMemory("r4", 5.5, Now))
    End With
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, July 6, 2017 4:20 PM
    Moderator

All replies

  • I have the following, at the moment I retrieve the reader with the strongest signal, what I want to do is exclude any reader which does not have 4 entries in the memory

    Hi Darren,

    According to your description, you want to  do is exclude any reader which does not have 4 entries in the memory. I'm not sure what your mean, if you just want to  query any reader that is not equal to 4 entries in the memory, you could add the code below.

    Dim query = From t In results Where t.Occurences <> 4
     

    If not, please provide more detailed info about your issue, or provide entire code that can reproduce your issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 6, 2017 2:30 AM
    Moderator
  • The list is made of TagLocationMemory, I want it grouped by reader but only where there are 4 entries for that reader. so each new StrongestReader will have at least 4 entries  

    so it would be 

    Reader 1         112    06/07/2017

    Reader 1         112    06/07/2017

    Reader 1         112    06/07/2017

    Reader 2         114    06/07/2017

    Reader 2         115    06/07/2017

    Reader 2         112    06/07/2017

    .....

    Thursday, July 6, 2017 7:39 AM
  • You may have to post to a forum that uses Linq. I know that the Count() has to come into play Where Count >= 4 on an AND.

    You have some CSS in there, but I don't know if the ASP.NET forum can help you with the Linq, but they should like maybe the EF forum in ASP.NET.

    http://forums.asp.net/

    Thursday, July 6, 2017 2:37 PM
  • Surely this forum is the best place for it - VB language? 
    Thursday, July 6, 2017 3:30 PM
  • Surely this forum is the best place for it - VB language? 

    You been here for over 6 hours. Maybe and maybe not that someone will show that can help. Just because you post to a specialized forum like EF,  that it means that someone can't help. I don't have the time nor the energy to figure it out for you, which I would use Linqpad maybe to do it. Some people can raddle the Linq off like it's nothing, but I am not one of them.
    Thursday, July 6, 2017 3:40 PM
  • Hi Darren,

    You should be able to insert a WHERE clause just before the select:

    Dim results = From t In m_lstTagLocationMemory
                  Group t By t.ReaderID Into byReader = Group
                  Order By byReader.Average(Function(x) x.SignalStrength) Descending
                  Where byReader.Count > 3
                  Select New StrongestReader() With
                  {
                     .ReaderID = ReaderID,
                     .AverageSignalStrength = byReader.Average(Function(x) x.SignalStrength),
                     .Occurances = byReader.Count(),
                     .LastPacketReceived = byReader.Max(Function(x) x.PacketDate)
                   }
    

    I tested with some junk data as follows and reader "r3" is excluded from the results:

    Dim m_lstTagLocationMemory As New List(Of TagLocationMemory)
    With m_lstTagLocationMemory
        .Add(New TagLocationMemory("r1", 2.5, Now))
        .Add(New TagLocationMemory("r1", 3.5, Now))
        .Add(New TagLocationMemory("r1", 4.5, Now))
        .Add(New TagLocationMemory("r1", 5.5, Now))
    
        .Add(New TagLocationMemory("r2", 2.5, Now))
        .Add(New TagLocationMemory("r2", 3.5, Now))
        .Add(New TagLocationMemory("r2", 4.5, Now))
        .Add(New TagLocationMemory("r2", 1.5, Now))
    
        .Add(New TagLocationMemory("r3", 2.5, Now))
        .Add(New TagLocationMemory("r3", 3.5, Now))
        .Add(New TagLocationMemory("r3", 4.5, Now))
    
        .Add(New TagLocationMemory("r4", 5.5, Now))
        .Add(New TagLocationMemory("r4", 3.5, Now))
        .Add(New TagLocationMemory("r4", 4.5, Now))
        .Add(New TagLocationMemory("r4", 5.5, Now))
    End With
    


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, July 6, 2017 4:20 PM
    Moderator
  • Thank you very much
    Friday, July 7, 2017 7:19 AM
  • Not sure what point you are trying to make here, I posted to the correct forum, if you don't know the answer that is fine, I am not forcing you. 

    Have a great day!

    Friday, July 7, 2017 8:03 AM
  • Not sure what point you are trying to make here, I posted to the correct forum, if you don't know the answer that is fine, I am not forcing you. 

    Have a great day!

    The correct forum to post to was the MSDN Linq forum that use to be around back in the day, but it was discontinued. The forum was established too early when Linq first came out and did not gain popularity like it should have done. But that was due the people's ignorance of Linq several years ago, and they didn't even know that Linq existed or what it was for, and unfortunately they still don't not really. 

     Now,  MSDN needs to bring that forum back into play, because too many people like you have questions about Linq, which I learned Linq using EF,  Linq-2-Entites and Linq-2-Objects back in the day. I don't use Linq that much anymore, and I am not the one that's going to write some code and serve it up on a silver platter either to answer someone's post.

    Now,  I myself wouldn't need any help to figure it out what you were trying to do, because I have done it long ago. Such issues as yours concerning Linq in todays environment , I would have used Bing or Google to go find the answer if need be, which would be in C# no doubt and not in VB.

    Friday, July 7, 2017 12:13 PM
  • I was not really after a reply it was rhetorical. However I asked the original question as I am working on lots of things (both C# and VB.net for the record) and the answer provided helped. I am not really interested in what you have or haven't done in the past, these forums are here to provide help and that is exactly what they have done. If your goal is to get the last word in, go ahead and knock yourself out - I wont be replying any more as I have marked this question as answered.



     
    Friday, July 7, 2017 1:12 PM