score:5
Accepted answer
not sure why people always force linq to be one liners.
var entities = context.messagingtemplateentities
.where(m =>
m.partyid == partyid &&
m.messagetemplatetypeid == messagingtemplatetypeid);
if(producttypepartyid != 0)
entities = entites.where(m.producttypepartyid == producttypepartyid);
decide for yourself which is more readable at a glance.
score:2
use && (m.producttypepartyid == producttypepartyid || producttypepartyid == 0)
instead of && m.producttypepartyid == producttypepartyid
score:4
var entities = context.messagingtemplateentities
.where(m =>
m.partyid == partyid &&
m.messagetemplatetypeid == messagingtemplatetypeid &&
(producttypepartyid == 0 ?
true :
m.producttypepartyid == producttypepartyid));
Source: stackoverflow.com
Related Query
- LINQ - CASE statement in WHERE clause
- Linq Query with a Where clause in an Include statement
- Linq with where clause in many-to-many EF Code First object
- When Where clause is used inside Linq statement produces different results than when used outside
- Entity Framework - LINQ statement with WHERE clause based on NULLABLE parameter
- C# - Linq optimize code with List and Where clause
- In a LINQ statement using closures, is it possible to specify a where clause within a closure?
- LINQ Statement with where clause makes the execution slow
- How to place an If/Else statement in a where clause in a linq query?
- How to convert multiple SQL LEFT JOIN statement with where clause to LINQ
- linq where clause not in select statement
- Where clause of LINQ statement to find instances of a string within a List<string> collection?
- DataTable Linq where clause to upper case comparison
- LINQ to SQL With the where with case statement
- How To Determine Which WHERE Clause Was True In A Linq Statement
- LINQ statement - WHERE clause for ENUM type
- using If statement inside where clause in LINQ
- LINQ where clause using Generic IQueryable source
- sql to linq where statement with case
- How to add a where clause to this Entity Framework Linq statement
- Expressing IN sql statement in Linq with computed string in where clause
- Subquery in Where Clause of LINQ statement
- Select statement on DataTable without where clause using Linq
- Place if statement inside a where clause in Linq for Entity framework
- WHERE clause in LINQ statement that pulls specific match or everything if no match found
- LINQ where clause with an if statement
- Case Statement In linq query on where condition using c#
- Linq statement fails with dynamic where clause
- EF LINQ to SQL is getting whole table from DB instead of selection - where clause contains list with Any statement
- Entity Framework dynamic linq where from generic source with dynamic where clause
More Query from same tag
- LINQ vs context.entity.Where()
- Getting a casting error for no visible reason
- How to return Generic.List<Anonymoustype> from a function in C#
- Linq Flatten List of List
- linq string.join
- View Lambdas in Visual Studio Debugger
- Linq to FileHelpers Class
- SQL to LINQ translation
- Where Clause is not applied when converting the query from LINQ (EF) to SQL
- calling EntityTypeBuilder<T>.HasKey via reflection for compound keys
- Retrieving data through associative class
- Average or max of strings
- LINQ to Sql group and sum in subquery but account for null values
- Why does Linq do joins differently
- Dynamic LINQ Query not working properly with Taxonomy field
- Convert SQL to LINQ: select the result into model
- Linq to query matching column from a table and populate in another table
- Run a specific task parallel using Await and Async
- Select 2 tables using LINQ
- Comparing 2 string Class Lists C#
- How to get Sum of all distinct values using LINQ
- Get subset x elements before and x elements after LINQ
- Converting foreach loop to LINQ query breaks code
- How to put XElement Document records inside a list of objects
- how to return the query result as a specific object type in LINQ
- combine several DbGeography polygons
- LINQ with JOIN, GROUP, SUM() and return a new SELECT
- LINQ on a double**
- Filter datatable if a column contains any one of the elements in an array
- Deferred execution vs ToList gives different results