Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

migrating to dbcontext LINQ where clause string parameter

I'm migrating my project from ObjectContext to DbContext and I have a problem with this sentence:

var query = db.Inventory.Where("it.IdState in {" + states + "}");

This works with ObjectContext but now I get an error at compilation time:

Error   CS1503  Argument 2: cannot convert from 'string' to 'System.Linq.Expressions.Expression<System.Func<Test.DAL.Inventory, bool>>' 

Seems not to be posible to do this now, I tryied to expose ObjectContext to do it, but I couldn't find the way

Any idea? Thank!

like image 510
Santiago Avatar asked Nov 16 '25 05:11

Santiago


2 Answers

You can (and should) strongly type these arguments as an expression.

var query = db.Inventory.Where(x => states.Contains(x.IdState));
like image 137
David L Avatar answered Nov 18 '25 18:11

David L


You kind of need to reverse it

var query = db.Inventory.Where(it => states.Contains(it.IdState));
like image 20
Jamiec Avatar answered Nov 18 '25 18:11

Jamiec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!