Home > other >  How to compare property of a first element with a second element in a List?
How to compare property of a first element with a second element in a List?

Time:10-13

I have a single list with objects. Let's call it ItemList. Objects have properties such as Name, Code, ParentCode and so on. Some of these items have the same ParentCode and I need to compare the first element's ParentCode with the next one in some conditions. So I am using an if condition like this:

if (ItemList.First().ParentCode != ItemList.ElementAt(1).ParentCode)

However, sometimes this causes some issues because the ItemList can have single element inside it and it throws argument out of range or index out of range exception. To overcome this I changed the code to this:

if (ItemList.Count >= 2 && ItemList.First().ParentCode != ItemList.ElementAt(1).ParentCode)

Sometimes I need to run the same method when the ItemList have only one element or the first element does not have the equal ParentCode with the second element so I use this condition:

if (ItemList.Count == 1 || ItemList.Count >= 2 && ItemList.First().ParentCode != ItemList.ElementAt(1).ParentCode)

All of these seems counterintuitive thus I am open to suggestions on making to code more maintainable and readable. Thanks in advance!

CodePudding user response:

A couple other Linq functions may help here, depending on the use-case. I'm not sure I fully understand why you'd only want to compare the first 2 elements though. What if it has 10 or 100 items? Only the first 2 matter? If it can only ever have 2 elements because of some other business logic, then consider creating a class that holds exactly 2 items, and put the "comparison/validation" logic inside that class. A constructor that accepts 2 parameters, first second instance, should ensure validity of the wrapper class.

Either way... for a purely LINQ solution...

  1. ItemList.GroupBy(x => x.ParentCode).Where(x => x.Count() > 1) ... will get you a list of "groups" that contain more than 1 duplicate ParentCode. Iterating that will provide you with a "Key" representing the ParentCode or whatever you group by.

  2. ItemList.Skip(1).FirstOrDefault() ... will get you the second element, if it exists, otherwise it will be the default for whatever type is in the list

CodePudding user response:

Is there any specific reason you have to use linq here? I feel like just referencing the objects directly is just as good of a solution.

if(ItemList.Count == 2)
{
  if(ItemList[0].ParentCode != ItemList[1].ParentCode)
  {
    ..do stuff..
  }
}

Yes, the code isn't as flat, but this is extremely readable. If you need to compare the 0th value to more than just 1st value, checking that the list's length is greater than or equal to 2 and using a for loop will work just fine.

if(ItemList.Count >= 2)
{
  for (var i = 1; i < ItemList.Count; i  )
  {
    if(ItemList[0].ParentCode != ItemList[i].ParentCode)
    {
        ..do stuff..
    }
  } 
} 

Only suggesting a non-linq solution because you didn't mention that it had to be linq. This code is still extremely readable and has no difference in performance. Also, as you mentioned in your post, none of this code seems counterintuitive.

  • Related