score:0
Maybe you're looking for something like this:
class Program
{
static void Main(string[] args)
{
Func<Person, bool> isInParis = BuildFunc("Location", "==", "Paris");
Console.WriteLine(isInParis(new Person { Location = "Paris"})); // Prints true
Console.WriteLine(isInParis(new Person { Location = "Venice"})); // Prints false
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public int Weight { get; set; }
public DateTime FavouriteDay { get; set; }
public string Location { get; set; }
}
private static Func<Person, bool> BuildFunc(
string propertyName,
string comparisonOperator,
string propertyValue)
{
var parameter = Expression.Parameter(typeof (Person), "person");
var property = Expression.Property(parameter, propertyName);
var constant = Expression.Constant(propertyValue);
Expression comparison;
switch (comparisonOperator)
{
case "<":
comparison = Expression.LessThan(property, constant);
break;
case ">":
comparison = Expression.GreaterThan(property, constant);
break;
default:
comparison = Expression.Equal(property, constant);
break;
}
return Expression.Lambda<Func<Person, bool>>(comparison, parameter).Compile();
}
}
Hope it helps!
Source: stackoverflow.com
Related Query
- LINQ Source Code Available
- .NET 4 Code Contracts: "requires unproven: source != null"
- creating Linq to sqlite dbml from DbLinq source code
- CLRSQL Aggregate function. LINQ Code works within CLR Function but cannot be deploy within Aggregate
- How to Transform in to one function using generics or dynamic?
- Transform "Age", ">", "18" in function "person.Age > 18"
- source code for LINQ 101 samples
- LINQ function to return list but compiler says function doesn't return a value on all code path
- List or Array of String Contain specific word in Html Source Code
- Common function to avoid code duplication for handling 2 different type of object
- c# Linq or code to extract groups from a single list of source data
- Showing joined tables in retrieve function in EF using Code First
- How to call an Sql User defined Function using Entity frame work Code first approach with LInq c#
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- Value cannot be null. Parameter name: source
- Apply function to all elements of collection through LINQ
- C#/Linq: Apply a mapping function to each element in an IEnumerable?
- Linq code to select one item
- C# - code to order by a property using the property name as a string
- How do I find the text within a div in the source of a web page using C#
- Roslyn failed to compile code
- call a function for each value in a generic c# collection
- Entity-framework code is slow when using Include() many times
- The data source does not support server-side data paging
- How are people unit testing code that uses Linq to SQL
- Entity Framework, Code First and Full Text Search
- Is there a LINQ function for getting the longest string in a list of strings?
- LINQ identity function
- What does this C# code with an "arrow" mean and how is it called?
More Query from same tag
- Get the rows where column/field sum equals a certain value
- Efficient way to find "hook words" in a list of words?
- LINQ Where filtering with lists
- Searching data in a list using linq
- How do "Where" and "Select" work in LINQ?
- Why isn't .Except (LINQ) comparing things properly? (using IEquatable)
- How to combine 2 lists using LINQ?
- How to use LINQ to filter property of child collection using (.Any())
- WCF Data Service : Many to many query
- linq index out of bounds
- Can I reasonably go five levels deep with a .Include in LINQ and a SQL Server 2012 Backend?
- How to intersect two different IEnumerable collections
- Bind the ServiceController objects to a List through LINQ query
- Dynamic LINQ aggregates on IQueryable as a single query
- In join how to segregate the select statement
- Removing a single item from an enumerable source when the items are equal
- How can i convert a SQL query into LINQ using group and having?
- Normalize ConcurrentDictionary of arrays
- How to use Union in Linq Query
- Linq Performance: (ElementAt,Count) vs (foreach)
- Linq left join when the class on the left side of equals can be null
- Sorting XDocument elements based on InnerXML
- Prevent duplicates when inserting
- C# XML- Linq Query where multiple elements of same name exist
- Select Max in Linq to Entities / Entity SQL with type conversion
- LINQ nested collection
- LINQ query NOT IN
- LINQ (C#) Sum a String in a HH:mm format
- QueryExtender Linq Orderby using TimeSpan.Parse
- How do I combine two lists to a List<KeyValuePair<>>?