使用LINQ的动态查询将无法正常工作 [英] Dynamic query with LINQ won't work
问题描述
我已经尝试了几种方法来执行一个简单的查询但没有成功。
I've tried a couple of ways to execute a simple query but without success.
var result = db.Persons.Where("surname = bob");
以上将给出错误:类型'中没有属性或字段'bob' Person'
var result = db.Persons.Where("surname = 'bob'");
以上将给出错误:字符文字必须包含一个字符
有没有人有这些问题,你做了什么?
不动态LINQ假设这样工作,或者是旧版本或什么?
Has anyone else had these problems and what did you do?
Isn't Dynamic LINQ suppose to work like this or are there older versions or something?
我已将 Dynamic.cs
添加到我的项目中,而我使用System.Linq.Dynamic
。
I've added the Dynamic.cs
to my project and I'm using System.Linq.Dynamic
.
推荐答案
进行比较时,需要两个 ==
并且为了避免注入使用以下重载:
When doing comparisons you need two ==
and also in order to avoid injection use the following overload:
var result = db.Persons.Where("surname == @0", "bob");
此外,我建议您下载并查看示例。 Scott Gu也有博客关于它。
Also I would recommend you downloading and looking at the examples provided with the product you are using. Scott Gu has also blogged about it.
这篇关于使用LINQ的动态查询将无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!