Dynamic Expressions in “Where” Clause - Linq to SQL?

You can just add Where() clauses if the condition you want is true var results = allCNT; if (!string. IsNullOrEmpty(disName)) results = result. Where(a => a.

Discipline_name == disname); if (!string. IsNullOrEmpty(statusName)) results = results. Where(a => a.

Status_description == statusName); dataGridView1. DataSource = results See comment below for one option for handling lots of filters. Another option is to use a helper method: T AddFilter(IQueryable results, string filterValue, Expression> predicate) { if(!string.

IsNullOrEmpty(filterValue)) return results. Where(predicate); return results; } Which you would use like this: var results = allCNT; results = AddFilter(results, disname, a => a. Discipline_name == disname); results = AddFilter(results, statusName, a => a.

Status_description == statusName); results = AddFilter(results, whatever, a => a. Whatever == whatever); // ... dataGridView1. DataSource = results.

You can just add Where() clauses if the condition you want is true... var results = allCNT; if (!string. IsNullOrEmpty(disName)) results = result. Where(a => a.

Discipline_name == disname); if (!string. IsNullOrEmpty(statusName)) results = results. Where(a => a.

Status_description == statusName); dataGridView1. DataSource = results; See comment below for one option for handling lots of filters. Another option is to use a helper method: T AddFilter(IQueryable results, string filterValue, Expression> predicate) { if(!string.

IsNullOrEmpty(filterValue)) return results. Where(predicate); return results; } Which you would use like this: var results = allCNT; results = AddFilter(results, disname, a => a. Discipline_name == disname); results = AddFilter(results, statusName, a => a.

Status_description == statusName); results = AddFilter(results, whatever, a => a. Whatever == whatever); // ... dataGridView1. DataSource = results.

Thank you very much! But what If have 8 combo-boxes? Isn't there a way to do something like: a=> a.

Status_descrition == everything && a. Displince_name==*specific*? – Nim Jul 12 at 10:16 One option would be to build one large Where() clause that looks like this: Where(a => (disname == "" || a.

Discipline_name == disname) && (statusName == "" || a. Status_description == statusName) && ...) – dahlbyk Jul 12 at 10:29.

You can add several Where clauses depending on what criteria you have, e.g. : var find = allCNT; if (!string. IsNullOrEmpty(disName)) { find = find. Where(a => a.

Discipline_name == disName); } if (!string. IsNullOrEmpty(statusName)) { find = find. Where(a.

Status_description == statusName); }.

Thank you very much! – Nim Jul 12 at 10:27.

You can just add Where() clauses if the condition you want is true...

You can add several Where clauses depending on what criteria you have, e.g.

Dynamic namespace which allows you to use string expressions in LINQ queries. It can be used to create dynamic queries but I don't like it because it is not type-safe. So let me give you an introduction to anonymous functions, lambda expression trees and the PredicateBuilder class.

These features can be used to create finder methods which can use dynamic strong-typed where-clauses as input parameters. First let us create a DataProvider class which contains an instance of a DataContext to the Northwind SQL Server database. Add a method GetEmployees which returns an IEnumerable of Employees.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions