The scenario is
void someFunc(object obj1 )
{
using ( var ctx = getContext() )
{
var list = ctx.MyTable.Where ( .... ).OrderBy( ... ).Select (
x => new PocoObject
{
string = x.String,
....
// Command = new MyCommand { prop1 = x } // working code
Command = new MyCommand { prop1 = x, objProp = obj1 } // problem line
}
).ToList();
//list.ForEach( x => x.Command.objProp = obj1 ); working code
}
}
The exception is
Unable to create a constant value of type ‘System.Object’. Only primitive types or enumeration types are supported in this context
changing the problem line with the code marked working code solves the issue
Is there any way to tweak it to make it simpler i.e. without the ForEach at the end ?
You can add an AsEnumerable() call which will prevent translation to SQL. Which will solve the problem since obj1 cannot be translated to SQL, since it is not a primitive type.