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 Query
- 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
More Query from same tag
- What are the two ways I can write this statement with LINQ?
- Group by Time(Only hrs part) in linq
- linq groupby plus select returning element type rather than value
- Best way to combine 2 lists
- How to use UserDefinedFunctionProvider.Invoke with udfs in a where clause
- Reusing projections in queries, the part after "select new"
- Two lambda expressions in a single query
- DbContext times out on remote server only
- LINQ for CRM how to use C# list in where clause
- C# : AsQueryable() vs AsQueryable<type>()
- Multiple clauses blocking the whole filter
- Optimizing list performance in C#
- selecting another row that has the same column value
- Unable to create a constant value of type 'WebApplication13.Models.Service'. Only primitive types or enumeration types are supported in this context
- How to combine / merge two StringCollection into one in C#
- How to filter related Content Parts within a Orchard CMS Query
- Use Linq to get the last order placed yesterday for each customer
- How to ignore records from table 1 (whoes fk-refrence must be present in table 2) but where condition does not satisfies
- Fast join of two big DataTables
- LINQ in C#. Check if one list contains elements from another one
- Game/Quote of the day - how to?
- Can I use LINQ to strip repeating spaces from a string?
- Retriving Sum of values under object collection using linq c#
- How can select first 3 item of list<>
- LINQ to SQL retrieve multiple columns from single datatable including grouping and sum
- LINQ Query for sum and aggregate data
- Querying xml child elements with prefixed namespace using LINQ to XML
- Determining the player that played the most games
- Error in Linq, Count()
- Connecting one table column to another table column in c# MVC2