i am trying to make left outer join for two lists where i am getting NullReference Exception.I am wondering why? Please help.
I have defined my sql query at the top(Comments) which works grea in sql server for the same joint but when i represented that query to LINQ it gives me error.
ContactList Having Data as
ContactId GroupId FirstName LastName Phone Email
GroupList Having Data as
GroupId GroupName
Here is the query i am trying for Left Outer Join.
/*
* select g.Id as 'Group Id',g.Name as 'Group Name',c.Id as 'Contact Id',
c.FirstName,c.LastName,c.Phone,c.Email from Groups g left outer join Contacts c
on g.Id = c.GroupId
*/
var joinedList1 = (from g in lstAllGroups.AsEnumerable()
join c in lstAllContacts.AsEnumerable()
on g.GroupId equals c.GroupId into temp
from rg in temp.DefaultIfEmpty()
orderby g.GroupId
select new
{
GroupId = g.GroupId,
GroupName = g.GroupName,
ContactId = rg.ContactId == null ? 0 : rg.ContactId,
FirstName=rg.FirstName == null ?String.Empty: rg.FirstName,
LastName = rg.LastName == null ? String.Empty : rg.LastName,
Phone = rg.Phone == null ? String.Empty : rg.Phone,
Email = rg.Email == null ? String.Empty : rg.Email,
}).ToList();
foreach (var item in joinedList1)
{
Console.WriteLine(item);
}
Note : In Left Outer Join, GroupList is the first Datasource and ContactList is the second datasource.