Reputation: 1458
I used joins and select query for passing dynamic values for the particular entity, Some fields are null in db. So i check the condition !=null
condition but the whole select statement is throwing error if i use that field.
My code as follows,
@placementId is as input value
IEnumerable<Submission> sub= from cc in confirmedCarrierRepository
join cs in carrierSubmissionRepository on cc.ConfirmedCarrierID equals cs.ConfirmedCarrierID
where cr.PlacementID == placementId
//orderby c.panel is missed
select new SubmissionEntity
{
PlacementId = placementId,
//The ComissionId is null in db,
CommissionId = cs.CommisionID.HasValue ? cs.CommisionID.ToString() : string.Empty
}
Another way:
CommissionId= cs.CommisionID!=null ? cs.CommissionID : new int()
I can't understand why. Even i check null condition also how could the whole select won't work. Please help me to find out.
Upvotes: 2
Views: 4253
Reputation: 2978
Use c# coalesce operator
CommissionId = (cs.CommisionID ?? 0).ToString();
Upvotes: 0
Reputation: 60493
If you're working in an IQueryable
world (meaning your linq must generate a db query), then ToString()
is not allowed.
You may use SqlFunctions.StringConvert
I'm not sure that HasValue
is allowed either (you may just try), but != null
is certainly allowed
Assuming CommissionId
is an int?
in your entity :
The "string" way
CommissionId = cs.ComissionId != null ? SqlFunctions.StringConvert((double)cs.CommmissionId) : string.Empty
The "int" way
CommissionId= cs.CommisionID ?? 0 // or default(int), but not new(int)
Upvotes: 1