score:2
Accepted answer
Something like (EDITED after re-reading the question) - but note that Expression.Invoke
doesn't work on EF in 3.5SP1 (but it is fine in LINQ-to-SQL):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
class Dept
{
public string DeptName { get; set; }
}
public static class Program
{
static void Main()
{
IList<char> chars = new List<char>{'a','b'};
Dept[] depts = new[] { new Dept { DeptName = "alpha" }, new Dept { DeptName = "beta" }, new Dept { DeptName = "omega" } };
var count = testing(depts.AsQueryable(), dept => dept.DeptName, chars).Count();
}
public static IQueryable<T> testing<T>(this IQueryable<T> queryableData, Expression<Func<T,string>> pi, IEnumerable<char> chars)
{
var arg = Expression.Parameter(typeof(T), "x");
var prop = Expression.Invoke(pi, arg);
Expression body = null;
foreach(char c in chars) {
Expression thisFilter = Expression.Call(prop, "StartsWith", null, Expression.Constant(c.ToString()));
body = body == null ? thisFilter : Expression.OrElse(body, thisFilter);
}
var lambda = Expression.Lambda<Func<T, bool>>(body ?? Expression.Constant(false), arg);
return queryableData.Where(lambda);
}
}
Source: stackoverflow.com
Related Articles
- Generating a LINQ query using Expression Trees
- Construct a LINQ GroupBy query using expression trees
- How to retrieve last 5 records using LINQ method or query expression in C#
- Dynamic linq query expression tree for sql IN clause using Entity framework
- LINQ - using a query expression to calculate data time difference
- How to reuse a linq expression for 'Where' when using multiple source tables
- How to call a lambda using LINQ expression trees in C# / .NET
- How can I write the following code more elegantly using LINQ query syntax?
- ORing LINQ Query, built using expression trees
- Linq sub query when using a repository pattern with EF code first
- Using LINQ query result for data source for GridControl c#
- Can take be used in a query expression in c# linq instead of using .Take(x)?
- Using LINQ to query an IDictionary with Lambda expression
- How can I check the number of calls to the database in LINQ query when using .NET Core and Code First?
- Linq order by using query expression
- Using an expression with a linq query
- Aggregate using LINQ with Query Expression syntax
- Unreachable expression code in LINQ query
- Convert LINQ query to string, send to remote server to parse into an expression using Roslyn?
- Building dynamic query in a loop using Expression trees
- How to best handle a search query that includes 0 to 3 parameters/filters but using a single LinQ to Entities expression
- LINQ Dynamic Query using Expression Tree
- The LINQ expression 'GroupByShaperExpression: could not be translated while using GROUPBY in LINQ query
- Generating a query with related data and conditions using LINQ expressions
- Using expression trees to create a custom order by in linq to entities
- How to restructure Linq query to avoid using Contains() inside an Any() lambda expression (need a ContainsAll())
- A LINQ question: map query expression to c# code
- How to write this linq query using Lambda expression
- Using LINQ to query three entitites. - Include path expression must refer to a navigation property defined on the type
- Using Lambda expression from a separate class in SELECT clause of LINQ query c# Projection
- Cosmos db sporadic 'requires a range index' exception when executing .net core client CreateDocumentQuery<T>
- How to get the data of specific user in one row
- Order by not-selected column
- dynamically add nodes in xml using linq
- Linq Group by With projection into a list
- C# LINQ query to select only first date in a month from List<DateTime>
- How I can do this using skip and take in linq
- Retrieving all values from IGrouping
- Read CSV with Linq
- Is there any requirement for model class in SQLite database?
- LINQ: Dinstinct and merge values
- Update Field with Linq in MVC
- How to: sum all values and assign a percentage of the total in Linq to sql
- LINQ RemoveAll instead of loops
- Can not get data after compare two tables
- Data has strange format after saved to csv
- Entity Framework 5 (pre-release) newbie issue about entity relations, etc
- Linq Paging using Skip and Take returning wrong result after first page
- "NOT EXIST" SQL equivalent to LINQ
- How to check "contain" here in IEnumerable list?