Chaining Linq Where clauses


Tikla

How do you chain "Where" clauses in linq based on different variable states. E, g; checkboxes for age ranges (21-30, 31-40, 41-50, 51-60, 60>)

We have a List<People>'People' that we need to filter based on checkboxes. Assuming List can't just be IEnumerable since it's already evaluated

Instead of doing this:

List<People> filteredPeople = new List<people>();
if(CB1.checked)
    filteredPeople = filteredPeople.Union(People.Where(inTheirTwenties))  //assuming method  inTheir20s filters correct  
if(CB2.checked)
    filteredPeople = filteredPeople.Union(People.Where(inTheirThirties)) ;
//...and so on

Is there a better way to solve this problem?

tom doss code

I packaged all of this into one Wherestatement and updated inTheirTwentiesthe method to make the individual look like this:

filteredPeople.Where(x => (CB1.checked && inTheirTwenties(x)) 
|| (CB2.checked && inTheirThirties(x)) ...);

Related


Chaining Linq Where clauses

Tikla How do you chain "Where" clauses in linq based on different variable states. E, g; checkboxes for age ranges (21-30, 31-40, 41-50, 51-60, 60>) We have a List<People>'People' that we need to filter based on checkboxes. Assuming List can't just be IEnumera

Two Where clauses of LINQ

Marvin Klein Does anyone know how I can chain my where clauses? I want to filter the items in the main list by the items found in the second result. I have the following sample code @foreach (var artikel in Controller.Artikel .Where(x => x.LieferantenArtik

Two Where clauses of LINQ

Marvin Klein Does anyone know how I can chain my where clauses? I want to filter the items in the main list by the items found in the second result. I have the following sample code @foreach (var artikel in Controller.Artikel .Where(x => x.LieferantenArtik

Two Where clauses of LINQ

Marvin Klein Does anyone know how I can chain my where clauses? I want to filter the items in the main list by the items found in the second result. I have the following sample code @foreach (var artikel in Controller.Artikel .Where(x => x.LieferantenArtik

Two Where clauses of LINQ

Marvin Klein Does anyone know how I can chain my where clauses? I want to filter the items in the main list by the items found in the second result. I have the following sample code @foreach (var artikel in Controller.Artikel .Where(x => x.LieferantenArtik

DexieJS (indexedDB) chaining multiple .where clauses

do not want I am using DexieJS to get data from IndexedDB. I have done the following tests with version 1.1.0 and 1.2.0. It works great for simple queries, but unfortunately I can't chain multiple where clauses. First, I tried var collection = db[table]; colle

DexieJS (indexedDB) chaining multiple .where clauses

University Teachers I am using DexieJS to get data from IndexedDB. I have done the following tests with version 1.1.0 and 1.2.0. It works great for simple queries, but unfortunately I can't chain multiple where clauses. First, I tried var collection = db[table

DexieJS (indexedDB) chaining multiple .where clauses

do not want I am using DexieJS to get data from IndexedDB. I have done the following tests with version 1.1.0 and 1.2.0. It works great for simple queries, but unfortunately I can't chain multiple where clauses. First, I tried var collection = db[table]; colle

DexieJS (indexedDB) chaining multiple .where clauses

do not want I am using DexieJS to get data from IndexedDB. I have done the following tests with version 1.1.0 and 1.2.0. It works great for simple queries, but unfortunately I can't chain multiple where clauses. First, I tried var collection = db[table]; colle

DexieJS (indexedDB) chaining multiple .where clauses

do not want I am using DexieJS to get data from IndexedDB. I have done the following tests with version 1.1.0 and 1.2.0. It works great for simple queries, but unfortunately I can't chain multiple where clauses. First, I tried var collection = db[table]; colle

DexieJS (indexedDB) chaining multiple .where clauses

do not want I am using DexieJS to get data from IndexedDB. I have done the following tests with version 1.1.0 and 1.2.0. It works great for simple queries, but unfortunately I can't chain multiple where clauses. First, I tried var collection = db[table]; colle

DexieJS (indexedDB) chaining multiple .where clauses

do not want I am using DexieJS to get data from IndexedDB. I have done the following tests with version 1.1.0 and 1.2.0. It works great for simple queries, but unfortunately I can't chain multiple where clauses. First, I tried var collection = db[table]; colle

DexieJS (indexedDB) chaining multiple .where clauses

do not want I am using DexieJS to get data from IndexedDB. I have done the following tests with version 1.1.0 and 1.2.0. It works great for simple queries, but unfortunately I can't chain multiple where clauses. First, I tried var collection = db[table]; colle

DexieJS (indexedDB) chaining multiple .where clauses

do not want I am using DexieJS to get data from IndexedDB. I have done the following tests with version 1.1.0 and 1.2.0. It works great for simple queries, but unfortunately I can't chain multiple where clauses. First, I tried var collection = db[table]; colle

DexieJS (indexedDB) chaining multiple .where clauses

do not want I am using DexieJS to get data from IndexedDB. I have done the following tests with version 1.1.0 and 1.2.0. It works great for simple queries, but unfortunately I can't chain multiple where clauses. First, I tried var collection = db[table]; colle

DexieJS (indexedDB) chaining multiple .where clauses

don't want I am using DexieJS to get data from IndexedDB. I have done the following tests with version 1.1.0 and 1.2.0. It works great for simple queries, but unfortunately I can't chain multiple where clauses. First, I tried var collection = db[table]; collec

DexieJS (indexedDB) chaining multiple .where clauses

don't want I am using DexieJS to get data from IndexedDB. I have done the following tests with version 1.1.0 and 1.2.0. It works great for simple queries, but unfortunately I can't chain multiple where clauses. First, I tried var collection = db[table]; collec

DexieJS (indexedDB) chaining multiple .where clauses

don't want I am using DexieJS to get data from IndexedDB. I have done the following tests with version 1.1.0 and 1.2.0. It works great for simple queries, but unfortunately I can't chain multiple where clauses. First, I tried var collection = db[table]; collec

DexieJS (indexedDB) chaining multiple .where clauses

don't want I am using DexieJS to get data from IndexedDB. I have done the following tests with version 1.1.0 and 1.2.0. It works great for simple queries, but unfortunately I can't chain multiple where clauses. First, I tried var collection = db[table]; collec

Where LINQ conditional clauses don't work

code sync Using: MVC 5, C#, VS 2013, EF6 with CodeFirst, SQL Server 2012 I have tried four different ways to get the data without any problems. IQueryable<vw_Results> qryResults = _db.vw_Results; The problem I'm having is that I have 13 filter options and the

Building OR expressions in WHERE clauses using Linq

Sergi Carmona I need to create a SQL Linq WHEREthat contains a clause of an ORexpression . Its operands are stored in a List. With this code I end up with an AND expression: private sigdaEntities db = new sigdaEntities(); List<string> list = new List<string>(

How to dynamically add or remove where clauses in LINQ

Ramesh Here is my LINQquery that needs to be updated to a dynamic where clause. If the value of the parameter idis 0, the where clause should return all records and the where clause should match records by id value. public async Task<IEnumerable<dynamic>> GetF

Generic Linq with specific where/order by clauses

Howard I want to create a generic method that will allow me to search the Sitecore 7 index using Linq to Sitecore (.Net 4.5). This will be used for various searches such as: Get the top 5 latest news pages Get the last 20 active pages Universal site search Etc

Performance of using 2 where clauses in LINQ

rexcfnghk In LINQ-to-Entities, you can query entities in the following ways: var students = SchoolContext.Students.Where(s => s.Name == "Foo" && s.Id == 1); I know that behind the scenes it will be converted to SQL similar to the following: SELECT * FROM Stud

Generic Linq with specific where/order by clauses

Howard I want to create a generic method that will allow me to search the Sitecore 7 index using Linq to Sitecore (.Net 4.5). This will be used for various searches such as: Get the top 5 latest news pages Get the last 20 active pages Universal site search Etc

LINQ expressions - dynamic From and Where clauses

chillydk147 I have the following list of integers and I need to extract various lists of integers containing numbers from a count containing 2-4 numbers. The code below will extract a list containing only 2 numbers. var numList = new List<int> { 5, 20, 1, 7, 1

Can SELECT and WHERE LINQ clauses be combined?

Bryce Here's what I do with the Selectuser and then delete all nullrecords : model.Users = users .Select(u => { var membershipUser = Membership.GetUser(u.UserName); return membershipUser != null

Add additional linq where clauses based on variables

Natalol I am trying to add additional where clauses to a linq query based on the result of a variable passed to a function. var allFeedback = from f in _unitOfWork.Feedback.All() join b in _unitOfWork.Bookings.All() on f.CourseBookingID equals b.Cours

LINQ expressions - dynamic From and Where clauses

chillydk147 I have the following list of integers and I need to extract various lists of integers containing numbers from a count containing 2-4 numbers. The code below will extract a list containing only 2 numbers. var numList = new List<int> { 5, 20, 1, 7, 1