c# - EF6 Lambda Query Error: Unable to create a constant of type ... Only primitive types of enumeration types are supported in this context -
i keep getting following error when execute query below. i'm not sure how else put in condition. haven't been able find same issue other questions have posed same error output.
unable create constant value of type 'project.models.bill'. primitive types or enumeration types supported in context.
the code:
var billresults = db.database.sqlquery<bill>("exec [dbo].[sp_getbills]").asqueryable(); var results = db.bills.select( => new { a.id, a.col1, a.col2, errorcount = (int) (billresults.where(x => x.billresultsid == a.id).count()) }).where(a => a.col1 == "test123");
i'd appreciate - been stuck past few hours on this.
thanks!
the error says cannot use a.id in expression, constant values. a.id depends on clause of db.bills query.
you can solve in 3 ways:
1) define class (let's call x) properties id, col1, col2 , errorcount. remove errorcount query , instead of creating anonymous object, create object of type x (errorcount not set). iterate on collection , set errorcount.
2) use cross apply using entity framework: read entity framework , cross/outer apply
it this:
from t1 in billresults t2 in db.bills.where(t2 => t2.id== t1.billresultsid ) select new { ... }
3) consider creating stored procedure , use cross apply join achieve need (read cross apply here https://www.mssqltips.com/sqlservertip/1958/sql-server-cross-apply-and-outer-apply/)
Comments
Post a Comment