EF 4查询 - 带有多个参数的问题 [英] EF 4 Query - Issue with Multiple Parameters

查看:95
本文介绍了EF 4查询 - 带有多个参数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  select * from customers where(@ CustomerName为null或CustomerName = @CustomerName)

这对LINQ to SQL有效: p>

  string customerName =XYZ; 
var results =
(来自c in ctx.Customers
其中(customerName == null ||(customerName!= null& c.CustomerName == customerName))
选择c);

但是上面的查询,当在ADO.NET EF中,不适合我;它应该由客户名称过滤,因为它存在,但它不。而是查询所有的客户记录。现在,这是一个简化的例子,因为我有很多领域,我正在利用这种逻辑。但它从来没有实际过滤,查询所有记录,并导致超时异常。但是最重​​要的是另一个查询做了类似的事情,没有任何问题。



任何想法为什么?似乎像我的错误,还是有解决办法?



谢谢。

解决方案

我还没有弄清楚,但是将其重写为一个proc来解决这个问题,所以这是我的解决方法,就像这样糟糕。


A trick to avoiding filtering by nullable parameters in SQL was something like the following:

select * from customers where (@CustomerName is null or CustomerName = @CustomerName)

This worked well for me in LINQ to SQL:

string customerName = "XYZ";
var results =
   (from c in ctx.Customers 
    where (customerName == null || (customerName != null && c.CustomerName == customerName)) 
    select c);

But that above query, when in ADO.NET EF, doesn't work for me; it should filter by customer name because it exists, but it doesn't. Instead, it's querying all the customer records. Now, this is a simplified example, because I have many fields that I'm utilizing this kind of logic with. But it never actually filters, queries all the records, and causes a timeout exception. But the wierd thing is another query does something similarly, with no issues.

Any ideas why? Seems like a bug to me, or is there a workaround for this? I've since switched to extension methods which works.

Thanks.

解决方案

I still didn't figure it out, but rewriting it as a proc fixed the issue, so that was my workaround, as bad as that is.

这篇关于EF 4查询 - 带有多个参数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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