动态Linq返回错误的结果

时间:2014-05-29 04:46:02

标签: c# linq dynamic-linq

我想通过linq搜索地址,我用2种方式:

的LINQ:

_db.Address.Where(address => address.FullName.Contains(text));

Dynamic Linq:

_db.Address.Where("FullName.Contains(@0)", text);

当我尝试:

- > text值是字符串(不包含space),2个查询中的结果是相同的,

- > text值为numberstring且内有space,查询1返回正确结果,第二个返回null

我没有Sql Profiler,所以我无法检查一般的sql。

如何解决此问题以及如何以正确的方式使用dynamic linq

1 个答案:

答案 0 :(得分:2)

我认为你需要这样做:

_db.Address.Where("FullName like @0", text);

或者你也可以这样做:

_db.Address.Where(a => SqlMethods.Like(a.FullName, "%"+text+"%"))

查看更多Details HERE

相关问题