Mickey Perlstein Mickey Perlstein - 1 year ago 86
C# Question

Linq outer left join what am I doing wrong?

reportMessages
are recieved from a report
cleandirs
are read from a storage.
I'm trying to verify contents of the report
for this i am doing an outter join on the
reportmessages
and the storage data

I limited the lists to 100, and still
m.count = 213
.

What am I doing wrong ?

var q = from r in reportMessages.Take(100)
join pp in cleanNoDirs.Take(100)
on r.FileName equals ("{:D2}{:D2}-{}.ps".format(pp.NameMin, pp.NameSec, pp.CameraMAC))
into ps
from p in ps.DefaultIfEmpty()
select new {
Uploaded = p,
Orig = r
};
var m = q.ToList();

Answer Source

You limit the records retrieved from each table to 100 but if you have a relationship of one-many or many-many then the result of the join will be more that 100 records. In the worst case if each record in the first collection matches all records in the second you will get 100*100 records in your result.

Instead, before materializing the results with the ToList limit to 100:

var result = (from r in reportMessages
              join pp in cleanNoDirs
              on r.FileName equals ("{:D2}{:D2}-{}.ps".format(pp.NameMin, pp.NameSec, pp.CameraMAC)) into ps
              from p in ps.DefaultIfEmpty()
              select  new {
                  Uploaded = p,
                  Orig = r
              }).Take(100).ToList();

For more on the different joins and how to write them in linq.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download