Quick LINQ question

L

Leon Mayne

I'm new to LINQ, and so having a small problem trying to left outer join two
generic lists of business objects.

I have a collection of department objects which I want to left join to a
collection of report group objects (based on an integer property called
ReportGroupId) and pull out the department name, id, and the report group
name (empty string if null). I came up with:

Me.gvwDepartments.DataSource = From d In colDepartments _
Group Join r In colReportGroups On d.ReportGroupId Equals
r.ReportGroupId _
Into rgGroup = Group _
From rg In rgGroup _
Select d.DepartmentId, d.Name, ReportGroup = If(rg Is Nothing,
String.Empty, rg.Name)

But this returns an empty set. Does anyone know what I'm doing wrong?
 
L

Leon Mayne

Leon Mayne said:
I'm new to LINQ, and so having a small problem trying to left outer join
two generic lists of business objects.

I have a collection of department objects which I want to left join to a
collection of report group objects (based on an integer property called
ReportGroupId) and pull out the department name, id, and the report group
name (empty string if null). I came up with:

Me.gvwDepartments.DataSource = From d In colDepartments _
Group Join r In colReportGroups On d.ReportGroupId Equals
r.ReportGroupId _
Into rgGroup = Group _
From rg In rgGroup _
Select d.DepartmentId, d.Name, ReportGroup = If(rg Is Nothing,
String.Empty, rg.Name)

But this returns an empty set. Does anyone know what I'm doing wrong?

Nevermind, I just found out I needed to add DefaultIfEmpty to the group
select:

From rg In rgGroup.DefaultIfEmpty
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top