Linq Conditional Where is the item

When searching for the linq conditional where condition, I found this article , how they are used is as follows:

var logs = from log in context.Logs
           select log;

if (filterBySeverity)
    logs = logs.Where(p => p.Severity == severity);

if (filterByUser)
    logs = logs.Where(p => p.User == user);

but I was wondering if this method is effective? How many queries will linq execute?

+5
source share
2 answers

You can use dynamic LINQ ( ScottGu article )

So, you can easily create a where clause in a string and then pass it to the Where method:

public string GetWhereClause()
{
string whereClause = "";
....
return whereClause
}

var logs = context.Logs.Where( GetWhereClause() );

Hope this helps;)

+2
source

, , . , - "". , (.. WHERE, ).

, LINQ , , , , , . SQL Server Profiler.

+5

All Articles