0
我有兩個LINQ的結果(結果,結果2)加入兩個LINQ結果
List<Employee> result= new List<Employee> {
new Employee { EmpId= 4, Date ='2014-02-26', EmpName= "abc"},
new Employee { EmpId = 5, Date = '2014-02-26', EmpName= "abc"},
new Employee { EmpId = 6, Date ='2014-02-26', EmpName= "abc"},
new Employee { EmpId= 7, Date = '2014-02-26', EmpName= "abc"} };
// Create a list of students.
List<Student> result2 = new List<Student> {
new Student { EmpId= 1, Date ='2014-02-26', StudentId = 9562 },
new Student { EmpId= 2,Date ='2014-02-26', StudentId = 9870 },
new Student { EmpId= 4,Date ='2014-02-26', StudentId = 9913 } };
我想下面的結果,我曾與各種查詢嘗試,但並沒有得到這樣的:
new Employee { EmpId= 4, Date ='2014-02-26', EmpName= "abc",StudentId=9913},
new Employee { EmpId = 5, Date = '2014-02-26', EmpName= "abc",StudentId=Null},
new Employee { EmpId = 6, Date ='2014-02-26', EmpName= "abc",StudentId=Null},
new Employee { EmpId= 7, Date = '2014-02-26', EmpName= "abc",StudentId=Null} };
我已經執行了下面的查詢,但得到一個異常「
(NullReferenceException被捕獲)未將對象引用設置爲對象的實例。
var res = (from r in result
join r2 in result2 on new { EmpId= r.EmpId, Date = r.Date.Value.Year } equals new { EmpId= Convert.ToInt32(r2.EmpId), Date = r2.Date.Value.Year } into list
from l in list.DefaultIfEmpty()
select new
{
EmpId= r.EmpId,
Date=r.Date,
EmpName=r.EmpName,
StudentId=l.StudentId
}).ToList();
請讓我知道,如果有人知道。
的'結果DefaultIfEmpty'是'null'參考類型爲空時。 – 2015-02-10 04:54:02