EF 4 Request - Multiple Parameter Issue

The trick to avoid filtering with null parameters in SQL was something like this:

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

This works 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 this above request, when in ADO.NET EF, does not work for me; it must be filtered by client name because it exists, but it is not. Instead, it queries all client records. Now this is a simplified example, because I have many fields with which I use this logic. But it never filters, does not request all records, and does not raise a timeout exception. But the strange thing is another request, does something similar, no problem.

Any ideas why? Does this seem to be a mistake for me, or is there a workaround for this? Since then, I have switched to extension methods that work.

Thanks.

0
source share
2 answers

I still did not understand this, but rewriting it as proc, fixed the problem, so this was my workaround, just as bad.

0
source

Have you tried it with the ternary operator in the where clause?

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

Source: https://habr.com/ru/post/944006/


All Articles