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")
};