Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF 4查询-具有多个参数的问题_C#_.net_Linq_Entity Framework_Entity Framework 4 - Fatal编程技术网

C# EF 4查询-具有多个参数的问题

C# EF 4查询-具有多个参数的问题,c#,.net,linq,entity-framework,entity-framework-4,C#,.net,Linq,Entity Framework,Entity Framework 4,避免在SQL中使用可为null的参数进行筛选的技巧如下: select * from customers where (@CustomerName is null or CustomerName = @CustomerName) 这在LINQ to SQL中对我很有效: string customerName = "XYZ"; var results = (from c in ctx.Customers where (customerName == null || (custo

避免在SQL中使用可为null的参数进行筛选的技巧如下:

select * from customers where (@CustomerName is null or CustomerName = @CustomerName)
这在LINQ to SQL中对我很有效:

string customerName = "XYZ";
var results =
   (from c in ctx.Customers 
    where (customerName == null || (customerName != null && c.CustomerName == customerName)) 
    select c);
但是当在ADO.NET EF中时,上面的查询对我不起作用;它应该按客户名称过滤,因为它存在,但它不存在。而是查询所有客户记录。现在,这是一个简化的例子,因为我有许多领域,我正在利用这种逻辑。但它实际上从未过滤、查询所有记录,并导致超时异常。但是wierd的问题是,另一个查询做了类似的事情,没有任何问题

你知道为什么吗?对我来说似乎是个bug,或者有解决方法吗?从那以后,我转向了有效的扩展方法


谢谢。

您在
where
子句中用三元运算符试过了吗

where (customerName == null ? true : c.CustomerName == customerName)

我仍然没有弄明白,但将其重写为进程解决了这个问题,因此这是我的解决方法,尽管这很糟糕。

这个查询很好(如果是多余的!),正如所写的那样。在林帕德试试。你的问题在于你没有在问题中输入的代码。是的;实际上,对于LINQtoSQL,有时如果我没有一个可为null的类型的冗余NOTNULL检查,它会出错,当我把它放进去时,它会正常工作。也许字符串没有那么大的问题,但我养成了一个习惯以防万一。不,我会试试看。谢谢。这并没有解决问题。不幸的是,它仍然忽略查询条件。