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:
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);
}
var klaus = from dynamic x in hans where x.ID < 3 select 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-. , .