score:2
a general solution for this sort of issue is to use predicatebuilder to dynamically construct the appropriate predicate.
first, build the predicate:
expression<func<order, bool>> predicate = predicatebuilder.true<order>();
if (filter.orderdate != null)
predicate = predicate.and(o => o.ord_date == filter.orderdate);
if (filter.custid != null)
predicate = predicate.and(o => o.custid == filter.custid);
...
and then you query becomes:
var filtered = db.orders.where(predicate);
var query = from o in filtered
join c in db.customers on c.id equals o.custid
select new {o,c};
score:0
maybe change it to this:
from o in db.orders
join c in db.customers on c.id equals o.custid
where o.ord_date == ( filter.orderdate ?? o.ord_date) &&
c.id == (filter.custid ?? c.id) &&
o.productid == (filter.productid ?? o.productid)
select new {o,c}
using the ?? operator keeps things a little tidier. it's basically like a coalesce operator for .net
other then that, i'm not sure what else you could change.
score:2
given that the conditions do not depend on the query, you could have the conditionals out of the query and build it gradually:
var query = from o in db.orders
join c in db.customers on c.id equals o.custid
select new {o,c};
if(filter.orderdate != null)
{
query = query.where(x => x.o.ord_date == filter.orderdate);
}
if(filter.custid != null)
{
query = query.where(x => x.c.id == filter.custid);
}
if(filter.productid != null)
{
query = query.where(x => x.o.productid == filter.productid);
}
Source: stackoverflow.com
Related Query
- Is there a better way of writing this Linq Query
- is there a better way to write this frankenstein LINQ query that searches for values in a child table and orders them by relevance?
- How to code this LINQ query in a better way
- Is there a better way to code this LINQ fragment?
- Is there a better way to achieve this with Linq or a better code
- Is there a better way to do this Linq query and function?
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- c# linq generated query length over the limit. Is there any way we could up lift this limit?
- Linq is this string.Compare in the query inefficient and is there a better way?
- How to write this LINQ Query in a better way
- Is there a better way of shortening this LINQ statement?
- Can i do this with dynamic LInq or is there a better way - Linq, C#, VS2008
- Is there a better way to do this LINQ statement block?
- C# LINQ Find List Inside Another List, Better way to code this than a foreach loop
- Is there a better way to code this Duplicate ID Checker?
- What is the performance optimum (or even better coding practise) for writing this Linq query
- Is there a better way to write this LINQ query?
- Better way to do this LINQ query
- Is there way to use Distinct in LINQ query syntax?
- Is there a better way of calling LINQ Any + NOT All?
- Is there a neat way of doing a ToList within a LINQ query using query syntax?
- Is there a better way to split this string using LINQ?
- Is there a way to simplify this linq
- Is there a better way to set a property common to a list of items in linq
- Is there a better way to combine these two Linq expressions?
- What is the return type for a anonymous linq query select? What is the best way to send this data back?
- Is there any way to optimize this LINQ to Entities query?
- Is there a more clear and concise way to express this statement with better LINQ?
- Is there a way to speed up this code that finds data changes in two XML files?
- Better way to write this linq query?
More Query from same tag
- How do I break up my data so I can feed it through to my view?
- Why "The LINQ expression 'x' could not be translated" ? I'm not using "Where()"
- How do I count the number of child collection's items using LINQ Method Syntax?
- MVC Selecting data with linq and passing to model
- Trying to create some dynamic linq
- Putting multiple values in a WHERE clause in Azure Mobile Services
- Elegant way to add a specific character after each char in a string
- LINQ adding extra logic to the WHERE clause
- Materialize all queryable objects
- Why List(Of T) doesn't have Count()?
- Linq, lambda - which statement is faster?
- Convert SQL subselect to LINQ
- Get string[] or process id's from process[] using LINQ
- Unsure on return type of linq to sql query
- MvcContrib Grid Sorting
- Resharper says to convert foreach to LINQ, but is it possible?
- Create a list with LINQ
- how to get distinct value in linq
- LINQ Query - Group multiple entities and sum
- LINQ with anonymous class/properties
- Understanding .AsEnumerable() in LINQ to SQL
- Populating a DropDownListFor with values from another model linked by a foreign key?
- GroupBy, OrderByDescending using Lambda in Linq
- .net core - combine a list of func with or to a single func
- Datediff sql query to linq lambda query
- Combine multiple lists into one list and order by amount
- Get nodes attributes in xdocument
- Linq distinct operation
- Calling a shared code from Page method and WebApi Controller with Razor Pages
- Entity Framework Linq SelectMany with condition