Using dynamic objects in encoded form of LINQ queries

I tried using expandoobjects in LINQ queries to be able to query properties created at runtime, such as headers from a csv file. All this worked fine if you type LINQ directly in the code, as in the example:

// initialize testdata
List<ExpandoObject> hans = new List<ExpandoObject>();
string[] names = {"Apfel", "Birne", "Banane", "Orange"};
int[] ids = { 1, 2, 3, 4 };
for (int i = 0; i < 4; i++)
{
   dynamic horst = new ExpandoObject();
   ((IDictionary<string, object>)horst).Add("Fruit", names[i]);
   ((IDictionary<string, object>)horst).Add("ID", ids[i]);
   hans.Add(horst);
}

// try some LINQ queries, both are working as intended
var klaus = from dynamic x in hans where x.ID < 3 select x;
//var klaus = hans.Where(x => x.ID < 3).Select(x => x);

Then I tried to read the query from the command line and create a dynamic LINQ query using a small modified version of the linq compiler for evaluation.

string expression = System.Console.ReadLine();
LinqCompiler lc = new LinqCompiler(expression);
lc.AddSource<ExpandoObject>("hans", hans);
IEnumerable<ExpandoObject> klaus = (IEnumerable<ExpandoObject>)lc.Evaluate();

Until I use the WHERE or ORDER BY statement, everything is fine, but if the request includes WHERE or ORDER BY, I get an error when compiling the encoding code in the linq compiler: CS1963: The expression tree may not contain a dynamic operation .

:

doRequestMethod.Statements.Add(new CodeMethodReturnStatement(new CodeSnippetExpression(Query)));

, codedom , , , LINQ-. , .

+3
1

, , LINQ Compiler, dynamic, # 4.0 Microsoft.CSharp.dll, , .

LINQ , IQueryable<T>. IQueryable<T> , IQueryable<T>, IEnumerable<T>. , LINQ , , IQuerybale<T> AsQueryable().

, :

public object DoRequest(System.Linq.IQueryable<System.Dynamic.ExpandoObject> hans) {
    return from dynamic x in hans where x.ID < 3 select x;
}

, IQuerybale<T> LINQ, Expression s. , , Expression dynamic.

, - LINQ Compiler IEnumerable<T> IQuerybale<T>, AddSource() :

public void AddSource<T>(string name, IEnumerable<T> source) 
{
    this.sources.Add(new SourceDescription(name, typeof(IEnumerable<T>), source));
}

, , , dynamic .

+2

All Articles