Left Outer Join - LINQ to Datatable

倖福魔咒の 提交于 2019-12-05 21:35:46

That is because here dtEmpSal is null (default case if sequence is empty):

from dtEmpSal in outer.DefaultIfEmpty() // dtEmpSal is null

When you are trying to call Field<T> extension on DataRow which is null, you get that exception:

dtEmpSal.Field<string>("Salary") // System.ArgumentException

Fix it with ternary operator. You was near, but checked wrong value:

from dtEmpRow in empInfo
join dtEmpSal in empSal
    on dtEmpRow.Field<string>("EmpId") equals dtEmpSal.Field<string>("EmpId")
into outer
from dtEmpSal in outer.DefaultIfEmpty()
select new
{
    Id = dtEmpRow.Field<string>("EmpId"),
    Name = dtEmpRow.Field<string>("EmpName"),
    // here instead of dtEmpRow you should check dtEmpSal
    Salary = (dtEmpSal == null) ? "(no salary)" : dtEmpSal.Field<string>("Salary")
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!