I keep getting an exception about Linq to Entities not supporting certaion query expressions like this:
MyDataContext db = new MyDataContext()
Brand = db.Brands.First(b => b.BrandId == Int32.Parse(brandIdString))
I’m not attempting to pass along the string parsing on to the entity store, I just want to parse that string into an integer for comparison. Is the only work around to do this before hand or am I totally off here?
The best option (at least for the example you gave) is to pull the operation out of your LINQ statement:
Explanation:
When you’re using LINQ, it wants to figure out ways to offload as much of the work as it can to your database query. In order to do that, it actually creates an expression tree of all the things you tell it to do in your various lambda expressions. Then when you want to run the query, it decides what SQL statement will be best for doing what you’ve asked it to do. This allows it to do some very fancy optimization. However, if it doesn’t know how to convert something into a SQL statement, it will get mad at you when you try to run the query.