1

Requirement

  1. I need to join Table1 and Table2
  2. The key between two tables are ID which is System.Guid type and is non-nullable value type
  3. If Table2.ID is null, I need to get null record from Table1.

LINQ syntax I wrote is as follows.

from records in DBContext.Table1
join history in DBContext.Table2 into recordhistory
from records in recordhistory.DefaultIfEmpty()
select (n => n);

The error I got is "The null value cannot be assigned to a member with type System.Guid which is a non-nullable value type."

Can someone advise me on this? Thank you very much.

ideacreek
  • 21
  • 3
  • "If Table2.ID is null, I need to get null record from Table1" What do you mean: can there be history records without ID? Or do you mean Table1 records without history records and, if so, do not select the Table1 record? – Gert Arnold Sep 17 '12 at 10:26

2 Answers2

0

Assuming you have an ID property, following should work as inner join:

var result = DBContext.Table1
              .Join(DBContext.Table2, t1 => t1.ID, t2 => t2.ID, (t1, t2) => t1);
loopedcode
  • 4,863
  • 1
  • 21
  • 21
  • What is with the last part? (t1,t2)=>t1 – Tormod Sep 17 '12 at 06:13
  • That is the record which is being returned to the result. If needed a new object can be constructed here which can contain combined properties of t1 and t2; e.g. new MyResult() { Property1 = t1.ID, Property2 = t2.Value } – loopedcode Sep 17 '12 at 15:00
0

I guess, the query, you provided, shoulg give an error, saying that on statement should be specified. So, I guess it should look somewhat like this:

from records in DBContext.Table1
join history in DBContext.Table2 on records.ID equals history.ID into temp
from recordhistory in temp.DefaultIfEmpty()
select new { Record = records, History = recordhistory };
horgh
  • 17,918
  • 22
  • 68
  • 123