开发者

Linq to Entities: Where clause containing ToString fails

开发者 https://www.devze.com 2023-04-07 14:18 出处:网络
I can do the following: var result = DB.Products.ToList()// .AsEnumerable() too .Where( p => p.ID.ToString() == ViewModel.ID);

I can do the following:

 var result = DB.Products.ToList()  // .AsEnumerable() too
              .Where( p => p.ID.ToString() == ViewModel.ID);

But it pulls all the products instead of only what I want, then filters locally. Without the ToList(), it fails to find/use the .ToString method in the projection. The ViewModel.ID is string from the client.

This question here talks about the same issue, minus the where clause, but the answer doesn't fix pulling every product locally.

My ViewModel.ID is string because knockout.js converts it from numeric to string if the user changes the value. I figured I would pursue this fir开发者_如何学JAVAst as it's probably easier to rule it out.


I think you are approaching the problem from the wrong direction. Convert ViewModel.ID back to integer (int.Parse), then you can offload the filtering back to the database. This is going to be far better than a workaround to cast p.ID to a string at the database, a move that would likely defeat any indexing on the value at the DB.

0

精彩评论

暂无评论...
验证码 换一张
取 消