score:8
you don't need to write two where
clauses; just add another condition to your first where
. the second condition should use any
function to search for the categories you're looking for.
iarticlerepository articlerepo = unitofwork.articlerepository;
list<article> articles = new list<article>(
articlerepo.getall()
.where(a => a.title == searchterm &&
a.categories.any(c => c.categoryid == 4))
.orderbydescending(a => a.createddate));
for multiple categories, suppose you have your categoryids in an int[]
or list<int>
named mycatidslist
. they you can change the categories clause in the above query to this:
a.categories.any(c => mycatidslist.contains(c.categoryid))
score:2
you don't need to create a new list and you can use several where expressions in one where clause. can you try the following code:
list<article> articles = articlerepo.getall()
.where(a => a.title == searchterm && a.categories.contains(category)).orderbydescending(a => a.createddate)).tolist();
score:3
there is an alternative syntax when using linq queries, which is more like sql. the code above is correct, but you may find this version more concise:
int categoryid = 4
iarticlerepository articlerepo = unitofwork.articlerepository;
var articlesquery = from article in articlerepo.getall()
from category in article.categories
where category.categoryid == categoryid
where article.title == searchterm
orderby article.createddate descending
select article
list<article> articles = articlesquery.tolist();
or its more common to do these all together in one step:
int categoryid = 4
list<article> articles = (
from article in articlerepo.getall()
from category in article.categories
where category.categoryid == categoryid
where article.title == searchterm
orderby article.createddate descending
select article
).tolist()
Source: stackoverflow.com
Related Query
- LINQ where clause using Generic IQueryable source
- Building a dynamic where clause for dynamic keywords or using IQueryable C# Linq
- Where clause not working on LINQ IQueryable query
- How to call a method in the where clause of a LINQ query on a IQueryable object
- How to negate a Where clause of an IQueryable
- Linq with where clause in many-to-many EF Code First object
- Mapping IQueryable where clause from DTO to Entity
- How to get the where clause from IQueryable defined as interface
- IQueryable where clause
- How to query by where clause with EF code first
- C# - Linq optimize code with List and Where clause
- How to extract a where clause expression tree from IQueryable
- Sum a IQueryable Column with where clause
- IQueryable where clause overriden in inherited class
- Add Where clause to IQueryable
- How to call a function inside a where clause of IQueryable
- LinqDataSource: How to assign IQueryable value to where parameters in code
- Entity Framework dynamic linq where from generic source with dynamic where clause
- IQueryable where clause generated from list that needs to be OR'd together
- where Clause in Asp.net is returning null exception in the code below
- Where IN clause in LINQ
- Linq: adding conditions to the where clause conditionally
- Multiple WHERE clause in Linq
- LINQ to SQL Where Clause Optional Criteria
- Dynamic WHERE clause in LINQ
- C# Linq where clause as a variable
- Add the where clause dynamically in Entity Framework
- How to dynamically add OR operator to WHERE clause in LINQ
- If condition in LINQ Where clause
- How to select array index after Where clause using Linq?
More Query from same tag
- Looking for non-matched items in a collection via Linq
- Expression of type 'T' expected when using Dynamic Linq
- linq query with many to many relationship
- How to list all subdirectories with certain date pattern in c#
- How can I concatenate the array content with its index value
- Using Enumerable.Contains method for null item checking
- Linq - how to filter dataset based on data in another table?
- Select doesn't work on IQueryable but does on IList
- Round double in LINQ to Entities
- Linq2Sql Clone an Object C#
- Selecting User from a table with LINQ statements/expression
- get distinct values in LINQ query
- Query on encrypted values using LINQ
- NullReferenceException when Linq'ing IEnumerable<XElement>
- Linq to DataTable not producing Distinct values
- Unable to execute LINQ Where clause with generic lambda expression
- Chaining / building LINQ query with an OR instead of AND
- ASP.Net membership provider
- When should I dispose of a data context
- How to check CONTAINS with multiple values
- How to validate LINQ queryable without database
- LINQ: Difference between 'Select c' and 'Select new (c...'
- Entity Framework - query all records across linking tables
- How to merge two lists based on a property?
- Split and get total count from string
- Dynamic Linq Contains to filter a List
- How to get Date without Time from DateTime while join tables
- How can I test if a string can be converted to a decimal in Linq?
- Login with linq with login (array) list dataset in another c# file
- Why am I getting different results filtering with foreach vs LINQ .Where()?