score:1
David Buchanan has posted a solution for this problem using reflection :
score:0
I'm not sure you can do this dynamically, but you can do it conditionally. Something like this:
switch(selected column name)
{
case "student_no":
q = q.where(p=>p.StudentNo == value);
break;
case "student_id":
q = q.where(p=>p.StudentId == value);
break;
}
You can iterate through your columns and keep building the wheres. The SQL won't be executed as long as none of the calls force the IQueryable to execute.
score:0
I think expression trees are the right way to do this, but I don't know them very well so I'm going to give you the alternate way I would have done this if I didn't feel like learning expression tree building..
public interface IFilter { IEnumerable RetreiveFilter(string filterValue); }
public class FirstNameFilter : IFilter
{
private const string FILTER_TYPE_NAME = "First Name";
public IEnumerable RetreiveFilter(string filterValue)
{
return _myData.Where(person => person.FirstName = filtervalue);
}
public override string ToString()
{
return FILTER_TYPE_NAME;
}
}
Create a class like this for each filter type, and then fill your dropdown with these filters, and when they type info into the filter text, it will execute against the ((IFilter)filterDropDown.SelectedItem).RetreiverFilter(filterTextBox.Text);
Source: stackoverflow.com
Related Articles
- How can I use drop down list value (String) to filter linq results?
- Filter a Dictionary by value where value is a list of string using LINQ
- Linq filter List<string> where it contains a string value from another List<string>
- Filter linq list on property value
- Simplest way to filter value from generic List in C# using LINQ
- LINQ query returns old results when source list is re-initialized
- LINQ filter list based on property value and filter the properties as well
- C# LINQ filter results based on property filed exist and value match
- LINQ filter by type on list from dictionary value
- C# Using LINQ to filter each Object with Max Value in List of Objects
- LINQ Filter results based on values between another list
- Filter linq query results using values from a list
- Linq extension. Change property value in source list
- LINQ filter list and exclude MAX value
- C# Left join linq results in ORA-12704 when selecting a default string value
- Filter a certain value in List according to Where using LINQ
- LINQ Get Results of object contained in another List by more than one Value
- Filter list of entity objects by string child object property using Linq Lambda
- Filter one list using index value obtained from another list using linq
- LINQ function to return list but compiler says function doesn't return a value on all code path
- How to filter a list by comparing a value from another list using Linq in C#?
- Filter a generic list based on a reflected value of its entries with linq
- Find a string in the list and change value of another property using linq (complicated)
- Filter a generic list with a dynamic condition string with linq
- Convert Database Linq Query Results to String List
- Filter a Generic list using LINQ using a PropertyName as a string literal - Using dynamics
- List or Array of String Contain specific word in Html Source Code
- LINQ filter string with value string
- Group linq results by value and group null or invalid values by empty string
- LINQ to check each property of a list object to see if it's value is equal to a string
- Linq to sql update multiple record from list of records
- Datatable group by sum
- Reading an XML document with Linq
- Create a filtered view of an observablecollection and display it in listbox
- Modify List<T> using Linq to Average rows with matching designator
- How to remove values from database that doesn't have a certain value
- Changing data structure using convertAll
- LINQ search similar string(names)
- Grouping a flat list and output hierarchical JSON
- How to retrieve element from IQueryable in Parallel Loop
- Join two dynamic Linq in Asp.net
- No new many to many connections are made in the database when saving an object in EF
- Is there a way to convert an IEnumerable into a collection of XElements?
- Add the navigation property value to strongly typed model data in ASP.NET-MVC-5 using LINQ
- If statement within linq
- How do I select a List from something like new Dictionary(int, List<Customer>);
- Join list into subclass and list<subclass>
- How to convert data in sql table into datetime and compare with other value in linq
- Sql Query converting to Linq Query By using Wcf Service Platefrom
- Conversion from System.Array to List<string> using LINQ, need to keep empty values