
SQL:
SELECT id,C.Name,S.Name,SalesManId FROM CUSTOMER C LEFT JOIN SalesMan S ON C.SalesManId = S.MasterId
what will be the equivalent lambda expression for the above query.
What I've tries is below:
public IEnumerable<CustomerModel> getDetails()
{
var cust = (from c in db.Customers.Include(c => c.CUSTOMERTYPE)
join sm in db.SalesMen on c.SalesManId equals sm.MasterId into l1
from sm in l1.DefaultIfEmpty()
//join ch in db.CUSTOMERHEADs.Include(ch => ch.CustomerDatas).Include(ch => ch.Customer).Include(ch => ch.CustomerProgress).Include(ch => ch.CustomerStatu) on c.Id equals ch.CustomerId into l2
//from ch in l2.DefaultIfEmpty()
select new
{
id = c.Id,
name = c.Name,
company = c.Company,
mb1 = c.Mb1,
address = c.Address,
date_ = c.Date_,
origin = c.Origin,
type = c.CUSTOMERTYPE.Type_,
salesman = sm.Name,
Salesid = sm.MasterId,
});
var details = new List<CustomerModel>();
foreach (var t in cust)
{
details.Add(new CustomerModel()
{
Id = t.id,
Name = t.name,
Company = t.company,
Mb1 = t.mb1,
Address = t.address,
Date_ = t.date_,
Origin = t.origin,
Type_ = t.type,
Salesman = t.salesman,
SalesManId = t.Salesid,
});
}
return details;
}
But I'm getting error when i want to retrieve SalesmanId Column.
The cast to value type 'System.Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.