c# - Issue related to IQueryable method (Cannot implicitly cast type 'long?' to 'long' -
i have iqueryable method below:
public iqueryable<vmtest> testmethod(long id) { return m in db.table1 join n in db.table2 on m.table1_id equals n.table1_id tabc c in tabc join o in db.table3 on m.table3_id equals o.table3_id m.table1_id.equals(id) select new vmtest{ field1 = m.xxx }; }
from code above, m.xxx long type , in vmtest there public long field1. there error m.xxx saying cannot implicitly convert type 'long?' 'long'.an explicit conversion exists (are missing cast?).may know what's wrong?
additional information:
if cast (long) in front of m.xxx, error dissapear problem occur when query returning no value it's wrong cast long null
you have several solutions:
- you can change vmtest class definition , replace field1 type long long?
- you can change table in database , transforms xxx null not null xxx become long
you can use condition in query like
new vmtest { field1 = m.xxx.hasvalue ? m.xxx.value : 0 }
more information on nullables here
Comments
Post a Comment