score:2
Accepted answer
reference this library:
using system.linq.dynamic;
and make your query like that:
string columnname = "active";
var lstcontacts = lstcontacts.where(columnname + " == true");
here is a working example:
using system;
using system.collections.generic;
using system.linq;
using system.linq.dynamic;
public class program
{
public static void main()
{
var lstcontacts = new list<contact>{
new contact{id = 1, active = true, name = "chris"},
new contact{id = 2, active = true, name = "scott"},
new contact{id = 3, active = true, name = "mark"},
new contact{id = 4, active = false, name = "alan"}};
string columnname = "active";
list<contact> results = lstcontacts.where(string.format("{0} == true", columnname)).tolist();
foreach (var item in results)
{
console.writeline(item.id.tostring() + " - " + item.name.tostring());
}
}
}
public class contact
{
public int id
{
get;
set;
}
public bool active
{
get;
set;
}
public string name
{
get;
set;
}
}
you can experiment with this .net-fiddle-here
Source: stackoverflow.com
Related Query
- How to perform a LINQ where clause with a dynamic column
- MVC Linq Query with dynamic column name in WHERE clause
- Building custom where clause with dynamic column input LINQ
- Entity Framework dynamic linq where from generic source with dynamic where clause
- How do you perform a left outer join with a where clause using linq lambda extension methods
- Dynamic Where Clause over relational tables with LINQ to SQL
- Linq to SQL: Where clause comparing a Nullable<DateTime> with a SQL datetime null column
- linq query with dynamic predicates in where clause joined by OR
- How to compare int with string in linq where clause
- Linq with where clause in many-to-many EF Code First object
- Dynamic LINQ (to entities) Where with nullable DateTime column
- how to create a pivot table with dynamic column using linq tree expression
- How to query by where clause with EF code first
- C# - Linq optimize code with List and Where clause
- How to use LINQ group by with where clause
- LINQ where condition with dynamic column
- How can I return a list or enumerable of rows where column value has changed with Linq
- how to use LINQ with dynamic paramters in orderby clause
- Where clause on dynamic column name in LINQ query?
- How to convert multiple SQL LEFT JOIN statement with where clause to LINQ
- Linq group by in sum column from with where clause
- Linq how to join tables with a where clause on each table and a count
- Linq Where clause with dynamic conditions
- LINQ dynamic query with Where clause
- Dynamic LINQ Query in LINQ to Entities , problem with WHERE clause
- How can I make this LINQ to SQL where clause with conditions faster?
- How to yield multiple objects with respect to a multi-valued column in Dynamic Linq
- how to write linq query with where clause to get records between 9 am to 5 pm
- linq query with dynamic where clause
- How to create a reusable function for LINQ where clause with extra param
More Query from same tag
- How to join deep data in the Entity Framework?
- Entity to LINQ upload CSV file where single rows can have multiple values in columns
- multiple orderby column of string by linq
- Linq to objects error: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
- Why this multi linq join code does not work?
- Cannot update specific item in list
- Retrieve attribute value from xml with c#
- How convert result of linq query to List(Of T)
- Linq comparing DataTable with List<String>
- Linq - get compare with transliterated value of fields
- How to select Closest distance from each category
- how to select entity from another entity using Linq?
- Filter a generic list with a dynamic condition string with linq
- How to Query List in C# for multiple values
- how can i order multiple digit string integers numerically and by shortest first? C#
- Update a nested property with Linq
- Linq getting a list from another list
- Efficiency of LINQ in simple operations
- Group List by property then return only another property of this grouped objects
- Comparing string array with starting values in another
- Find Count using lambda Expression
- Subqueries in linq instead of recursion
- How to ignore joining objects in LINQ Select statement to pull record
- Search using Linq from the view
- Syntax to fill linq array?
- Group rows and get max from each group.
- Return object from Stored procedure in Linq
- Find line-by-line difference using LINQ
- Joining two tables using LINQ
- F# groupBy - System.Exception : unrecognized method call