使用LINQ动态查询将无法正常工作 [英] Dynamic query with LINQ won't work

查看:187
本文介绍了使用LINQ动态查询将无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了几种方法来执行一个简单的查询,但没有成功。

I've tried a couple of ways to execute a simple query but without success.

var result = db.Persons.Where("surname = bob");  

以上会给出错误:无属性或字段'鲍勃'存在于类'人'

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");

此外,我建议你下载并查看范例提供你的产品使用。斯科特谷也已<一个href=\"http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx\">blogged一下吧。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆