0

I have two lists of Student Object in my code List<student> list1 and List<student> list2. The student object has the following properties. FirstName LastName University

I have the following method where I would like to compare the value of the respective property between the objects in the two list using LINQ. I found a few examples in LINQ that showed how to compare two values in a list of integers or string, but could not find any examples that compares the property value of the objects in the List.

`private CompareList(ref List<student> L1,ref List<student> L2)
 {
   // compare FirstName of L1 to L2
      ......

 }`

How would I go about doing this? Thanks!

Irfarino
  • 522
  • 1
  • 8
  • 13
  • Firstly, you need to actually try and implement the method before you ask on SO. Secondly, why are you passing your lists by reference? Lastly, why do you feel it's necessary to use Linq for this? – Ant P May 05 '14 at 00:17
  • Hi Ant, I would like to know an efficient way to implement the method that is why I asked the question. If not LINQ than how, that is why I asked the question, so that I would implement the method in a proper way. I used ref so as to not create unnecessary variable again trying to be efficient. thanks – Irfarino May 05 '14 at 00:19
  • 2
    I don't think you understand what `ref` does. It doesn't prevent the creation of unnecessary variables. It just means that if you were to assign a new list to the variables passed by `ref` it would change the reference in the calling code. If you omit the `ref` then you cannot change the reference in the calling code. A new variable is created on the stack for each call regardless if you use `ref` or not. – Enigmativity May 05 '14 at 01:20
  • Hi Enigmativity, I do understand what ref does, it set's the variable L1 to refer to the same object without creating a new copy of the object, I do not need to use the ref keyword but I am just being prudent, if the list of object is big. – Irfarino May 05 '14 at 01:43
  • 1
    @Irfarino, you really don't understand what `ref` means. A `List` (or any `List` - and types are conventionally Pascal cased) is a reference type, which means it resides on the heap and variables (or method parameters) only hold the pointer to it. When you use `ref` you are passing the pointer to the pointer. You reasoning is valid for value types (`struct`) that should never be mutable or so big that you consider passing them to methods as reference because of its size. – Paulo Morgado May 05 '14 at 06:08
  • @Irfarino To try and simplify what Paulo is saying, the variable *already* refers to the same object - you do not duplicate the object when passing a list. You need to understand the difference between *reference types* and *passing by reference*. Then you need to stop passing everything by reference, because it is one of the most misunderstood and abused constructs in C#. – Ant P May 05 '14 at 09:00
  • @Enigmativity,@Ant P,@Paulo Thanks for the input, and here I thought I knew enough about C#, that is why I had asked the question to get proper guidance, thank you for all your input. Cheers! – Irfarino May 05 '14 at 12:50
  • @Irfarino - And while we're at it ... Doing `@` to message people only messages the first person. The `@Ant P` and `@Paulo` didn't ping the other guys. You need to make separate comments to get to them. – Enigmativity May 06 '14 at 00:50
  • @AntP - See above messages. – Enigmativity May 06 '14 at 00:50
  • @PauloMorgado - See above messages. – Enigmativity May 06 '14 at 01:31

3 Answers3

3
public static bool Compare(ref List<Student> list1, ref List<Student> list2)
{
        return Enumerable.SequenceEqual(list1,list2, new MyCustomComparer());
}

public class MyCustomComparer : IEqualityComparer<Student>
{
    public bool Equals(Student x, Student y)
    {
        if (x.FirstName == y.FirstName && x.LastName == y.LastName && x.University == y.University)
            return true;
        return false;
    }

    public int GetHashCode(Student obj)
    {
        throw new NotImplementedException();
    }
}
DBK
  • 403
  • 4
  • 13
1

You didn't say what exactly you want to check, but if you just want to know if two collections are equal then I guess you could check it this way:

bool isAnyElementFromL1NotInL2 = L1.Select(x => x.Name).Except(L2.Select(y => y.Name)).Any();
bool isAnyElementFromL2NotInL1 = L2.Select(x => x.Name).Except(L1.Select(y => y.Name)).Any();
bool areL1AndL2TheSame = !isAnyElementFromL1NotInL2 && !isAnyElementFromL2NotInL1;

See also this answer by Jon Skeet

Or this question.

Community
  • 1
  • 1
Lech Osiński
  • 512
  • 7
  • 14
  • Hi Lech, thank you for the answer, I saw Jon' answer earlier, the list I have is not just a value list it is a list of Object, and I would like to compare the proprties of the objects in the first list with the properties of objects in the second list,I am not sure if it is a straight comparision as you describe, however I will try your method and see if it works. Thanks! – Irfarino May 05 '14 at 01:46
1

The best ways to do it would be to either the student (types are conventionally Pascal cased) being comparable by implementing the IComparable<student>/IStructuralComparable or IEquatable<student>/IStructuralEquatable or by creating a comparer class implementing IComparaer<T>.

Paulo Morgado
  • 14,111
  • 3
  • 31
  • 59
  • Hi Paulo,Thanks for the answer, I came across the IEquatable interface when I did a little bit of research, and I will probabely implement the interface. – Irfarino May 05 '14 at 12:52