score:3
aaah, i see what you are asking... and you're really entering some very murky waters, one of the few areas that the .net reflection libraries aren't beautiful to work with. you have to create a call expression to call queryable.where() on the queryabledata object, and create a new query using that expression... the problem is that getting a generic version of a method in .net isn't necessarily the easiest thing you've ever run across in your life:
methodcallexpression call = expression.call(
null, // calling queryable.where(), extension method, not instance method
getgenericmethod<string>(typeof(queryable), "where", typeof(iqueryable<string>), typeof(expression<func<string,bool>>)),
expression.constant(queryabledata),
expression.lamda(
e1,
p1)
);
iqueryable<string> res = queryabledata.provider.createquery<string>(call);
you would also have to define getgenericmethod (you can find better implementations for this online at other places, this is really quite a simple approach):
private static methodinfo getgenericmethod<t>(type type, string name, params type[] paramtypes)
{
methodinfo[] methods = type.getmethods(name);
foreach(methodinfo mi in methods)
{
if(!mi.isgenericmethoddefinition) // or some similar property
continue;
if(mi.getgenericarguments().length != 1)
continue;
if(mi.getparameters().length != paramtypes.length)
continue;
methodinfo genmethod = mi.makegenericmethod(new type[]{typeof(t)});
var ps = genmethod.getparameters();
bool isgood = true;
for(int i = 0; i < ps.length; i++)
{
if(ps[i].parametertype != paramtypes[i])
{
isgood = false;
break;
}
}
if(isgood)
return genmethod;
}
return null;
}
there are almost undoubtedly a few errors in there, but i hope you can see where to go from there...
score:0
just to give my solution:
string[] arr = {"s1","s2","s3"};
iqueryable<string> queryabledata = arr.asqueryable<string>();
parameterexpression pe = expression.parameter(typeof(string), "company");
expression right = expression.constant("on");
expression left = expression.call(pe, typeof(string).getmethod("contains"), right);
methodcallexpression e2 = expression.call(
typeof(queryable),
"where",
new type[] { queryabledata.elementtype },
queryabledata.expression,
expression.lambda<func<string, bool>>(left, new parameterexpression[] { pe }));
iqueryable<string> res = queryabledata.provider.createquery<string>(e2);
Source: stackoverflow.com
Related Query
- Linq expression tree string comparison
- how to translate a linq expression into sql string use c# code
- How to create LINQ Expression Tree to select an anonymous type
- Issues Doing a String Comparison in LINQ
- How do I create a Linq expression tree with an F# lambda?
- Can you reverse order a string in one line with LINQ or a LAMBDA expression
- Parsing a string C# LINQ expression
- Print out Linq Expression Tree Hierarchy
- Comparison : LINQ vs LAMBDA Expression
- How to build a dynamic AND OR linq expression tree in a loop
- Identify an event via a Linq Expression tree
- Dynamic LINQ DateTime Comparison String Building - Linq To Entities
- Dynamic linq query expression tree for sql IN clause using Entity framework
- LINQ Expression Conversion / Concat from Int to string
- Net core EF 3.1 LINQ string comparison no longer working
- How to reuse a linq expression for 'Where' when using multiple source tables
- Case insensitive string compare in LINQ expression
- LINQ Expression Tree Any() inside Where()
- How to re-wrap a Linq Expression Tree
- LINQ Source Code Available
- Running a regular expression on a String using Linq
- Build expression tree for LINQ using List<T>.Contains method
- Creating a LINQ Select expression dynamically from string column names
- Access property in lambda expression from string when using LINQ
- what is the Linq expression tree for setting a property of an object?
- LINQ to Entities OrderBy Expression Tree
- Creating a tree with LINQ expression
- how to create a pivot table with dynamic column using linq tree expression
- Unable to get double.TryParse to work in Linq Expression Tree
- LINQ Dynamic Expression API, predicate with DBNull.Value comparison
More Query from same tag
- Database-side ordering by distance from strings in C#
- Load XML into CLR Objects via LINQ in C#
- LINQ: several DeleteOnSubmit order behaviour on SubmitChanges
- Entity Framework Query Timeout
- List<a> to List<b> converting when a and b have the same structur?
- scala -> use .net (linq) and java code base in the same program?
- What's the benefit of .Cast over .Select?
- How to select specific columns in one-to-one relationship table along with few direct columns in Entity Framework Fluent API
- Select the complete row of a maximum value column
- linq to entities does not recognize the method int32 toint32
- Exact Match Array in Mongo Query
- How to create list of items from another list
- LINQ query is slow
- Recode Datatable Column with Linq
- Get distinct results using LINQ GroupBy for complex entity relationships?
- LINQ to entities Child queries where Parent has certain properties
- LINQ Min() and Max() optimisation
- get associated records in ms dynamics crm
- Is it possible to create anonymous type in LINQ extension methods in C#?
- Select rows based on group by counts
- Using Linq to find duplicates but get the whole record
- Split Too Long String
- how can this SQL be done in LINQ?
- Linq Expression to generic - how
- How to use null in join condition in linq
- How to increase speed while getting Images and text from database?
- Get String (Text) before next upper letter
- creating new object in select based on linked table in linq
- How to get all properties with an specific name and create a List<KeyValuePair<string,string>>
- Complex LINQ Query with Groupings (need some help)