0

I have a list of custom classes below.

protected class Ringing
{
    public int Id { get; set; }
    public int Rssi { get; set; }
    public byte[] MacAddress { get; set; }
}

private List<Ringing> Ringings { get; set; } = new List<Ringing>();

Then, I put in the data.

Ringings.Add(new Ringing
{
    Id = 2,
    Rssi = 1,
    MacAddress = new byte[] { 0x12, 0x34 },
});
Ringings.Add(new Ringing
{
    Id = 3,
    Rssi = 2,
    MacAddress = new byte[] { 0x12, 0x34 },
});
Ringings.Add(new Ringing
{
    Id = 4,
    Rssi = 4,
    MacAddress = new byte[] { 0x56, 0x78 },
});
Ringings.Add(new Ringing
{
    Id = 2,
    Rssi = 10,
    MacAddress = new byte[] { 0x56, 0x78 },
});

Here, I want to leave only one object with the lowest RSSI value per MAC address. The expected result is:

Id = 2, Rssi = 1, MacAddress = { 0x12, 0x34 },    
Id = 4, Rssi = 4, MacAddress = { 0x56, 0x78 }

How do I query it?

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
playground
  • 75
  • 6

2 Answers2

0

To group your list based on the byte[] property MacAddress you can convert the byte array to a string with BitConverter.ToString(x.MacAddress)

//group by MacAdress
Console.WriteLine("Grouped by MacAddress Results:");
var byMac = ringings.GroupBy(x => BitConverter.ToString(x.MacAddress)).Select(x => x.OrderBy(y => y.Rssi).First());
foreach (var byMacItem in byMac)
    Console.WriteLine(byMacItem.Id + " | " + byMacItem.Rssi + " | " + BitConverter.ToString(byMacItem.MacAddress));

Here a working example: https://dotnetfiddle.net/BzBQDf

The example also contains a solution to group by the Id, since at first I wasn't sure how the grouping should be, but after some tinkering I am positive you want to group by MacAddress.

Rand Random
  • 7,300
  • 10
  • 40
  • 88
  • It works fine, but why do I need to convert MacAddress to string when grouping? – playground Apr 05 '21 at 00:31
  • @playground - because the default equality comparer for an array doesn’t compare if the entries of two arrays are equal but only does check if the reference is equal, by converting the byte arrays to string you are using the string comparer which than does compare the content of the string instead of reference equality, as the user „Drag and Drop“ mentioned you can also pass in a custom comparer which you could use to implement you own comparison between arrays – Rand Random Apr 05 '21 at 08:56
-1

Class Ringing:

 public class Ringing
{
    public int Id { get; set; }
    public int Rssi { get; set; }
    public byte[] MacAddress { get; set; }
}

Test:

  class Program
{
    static void Main(string[] args)
    {
        List<Ringing> ringing = new List<Ringing>();
       
        ringing.Add(new Ringing
        {
            Id = 2,
            Rssi = 1,
            MacAddress = new byte[] { 0x12, 0x34 },
        });
        ringing.Add(new Ringing
        {
            Id = 3,
            Rssi = 2,
            MacAddress = new byte[] { 0x12, 0x34 },
        });
        ringing.Add(new Ringing
        {
            Id = 4,
            Rssi = 4,
            MacAddress = new byte[] { 0x56, 0x78 },
        });
        ringing.Add(new Ringing
        {
            Id = 2,
            Rssi = 10,
            MacAddress = new byte[] { 0x56, 0x78 },
        });


        var groupByMac = ringing.GroupBy(x => BitConverter.ToString(x.MacAddress)); //Group by Mac address.
        var lowestRsi = groupByMac.Select(x => x.OrderBy(y => y.Rssi)).First();  //Select lowest rssi by doing order

        foreach(var rssi in lowestRsi)
        {
            Console.WriteLine("Result:");
            Console.WriteLine("Id:" + rssi.Id);
            Console.WriteLine("Rssi:" + rssi.Rssi);
            Console.WriteLine("MacAddress:" + BitConverter.ToString(rssi.MacAddress));
            Console.WriteLine("*******");
        }

        Console.ReadLine();
    }
}

Please view explanation here video

  • Please try to improve your answer so you explain why your solution is a solution and how it works. Do not link to any youtube channel that contains a video, instead try to explain everything on this platform in text form. – Markus Safar Apr 02 '21 at 11:40