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 Articles
- 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?
- How do get a strongly typed single value using LINQ
- How to get elements value with Linq To XML
- Rule based grouping of data into segments LINQ
- How to select a collection nested within another collection in LINQ
- Entity Framework: Max. number of "subqueries"?
- Linq To SQL Join
- Linq to SQL with multiple Unions
- How can you retrieve all records of certain subtype dynamically with Linq to Entities?
- How to populate a list<list<T>> with 100 empty List<T>s without a loop?
- LINQ Pad- Can you do nested projections? Limiting selected columns while maintaining heirachical grid output style
- Performing batch update in self referencing table
- Next query is not executed until I materialize previous query by Entity Framework
- Linq query with left join having multiple tables
- Ask for a linq to manipulate arrays of array
- Query Slow in Linq, Fast in LinqPad, SQL Management Studio and SQL Profiler
- Why does my SQL query != "Some Value" not return any rows where NULL?
- How to use Linq to select information from a collection into another concrete class I made?
- How can I create a JSONPath filter expression to search using contains keyword
- Association on LINQ on Entity Framework
- LINQ XML Getting combobox value from another form c#