Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Linq produces redundancies?

I refer to the Examples in How to: Combin Data with Linq by using joins. We have two Lists the first holds person objects (First- and Lastname). The second List holds Pet objects (Name) that holds a person object (pet owner). One Person can own >= 0 pets.

What happend now is I performed the group join

Dim result1 = From pers in people
            Group Join pet in pets
            on pers Equals pet.Owner
            Into PetList = Group

LinqPad shows me the result:

LinqPad Output

This looks to me like Linq is producing a lot of redundancies (but I might be wrong here!). The first result object would hold the person object three times. Two questions arises here for me as a Linq nooby (but maybe I read the output not the right way):

  1. Are the person objects references? Unfortunately I couldnt find anything about it.
  2. Following the example mentioned above the query continues with
Select pers.FirstName , pers.LastName,
PetName = If(pet is Nothing, String.Empty, pet.Name)

If we have all information about the Person Object in the PetList, why not just query this object? In my opinion we dont need the pers Object anymore.

like image 627
ruedi Avatar asked Sep 30 '22 22:09

ruedi


1 Answers

Yes, those Person objects are references. LINQ is not cloning Person objects.

The reason for that output is that LINQPad is trying to show you who that person is. Without repeating a walk of its properties and fields, it would be hard to tell what person it was referring to.

like image 171
Joe Albahari Avatar answered Oct 05 '22 07:10

Joe Albahari