Reputation: 2168
I am trying to join 3 tables together using LINQ to SQL but I am running into an issue. Here is my code.
(from table1 in dc.Table1
join table2 in dc.Table2 on table1.Id equals table2.Id
join table3 in dc.Table3 on table2.Id equals table3.Id into merged
from rt in merged.DefaultIfEmpty()
select new { table1.value1, table1.value2, table2.value1, table2.value2, rt.value1, rt.value2 }).ToList();
The error I'm getting is
The null value cannot be assigned to a member with type System.Double which is a non-nullable value type.
I'm not sure I'm doing it correctly in the first place. Basically I want to do an inner join on Table1 and Table2 then a left outer join on that with Table 3.
Upvotes: 0
Views: 165
Reputation: 382
try this:
var result = dc.Table1
.Join(dc.Table2, a => a.Id, b => b.Id, (a, b) => new { Id = a.Id, A = a.value1, B = a.value2, C = b.value1, D = b.value2})
.Join(dc.Table3, a => a.Id, b => b.Id, (a, rt) => new { a.A, a.B, a.C, a.D, E = null != rt ? rt.value1 : null, F = null != rt ? rt.value2 : null})
.ToList()
Upvotes: -1
Reputation: 5689
Regenerate your DB model.
If you have columns in your db that are nullable and they get mapped to value types in .net (int
, double
, decimal
, etc), Linq to SQL will generate them as nullable types, so you won't have to change your linq to sql statements to take the null into account, unless that is actually part of your query.
Upvotes: 1
Reputation: 3751
Did you try this?
(from table1 in dc.Table1
join table2 in dc.Table2 on table1.Id equals table2.Id
join table3 in dc.Table3 on table2.Id equals table3.Id into merged
from rt in merged.DefaultIfEmpty()
select new { table1.value1, table1.value2, table2.value1, table2.value2, rt.value1 == null ? 0 : rt.value1, rt.value2 == null ? 0 : rt.value2}).ToList();
Upvotes: 1