I have this code :
var res1 = dtData.AsEnumerable()
.Where(...)
.Select(f => new { val = f["PremiumAfterUWDiscount"].ToDecimalOrZero(),
idpolicy = f["IdPolicy"].ToString() })
.FirstOrDefault();
however , since this returns an anonymous type , its default value is null .
I want it to act as FirstOrDefault for int type.
so if there is no record , it will return 0 ( default behavior as int).
is it possible ?
p.s. ( of course i can check it in a condition but still , i prefer the linq way).
Return an anonymous type that signifies “nothing” and either use the null coalescing operator:
Or the
DefaultIfEmptyextension method:You would only be able to return an
intin place of an anonymous type if you in fact return anobjectand cast later on (as per @recursive’s answer), but this to me seems counter-productive.FirstOrDefaultdoes not offer a way to specify what “default” is.