sma*_*man 10 .net c# linq iqueryable linq-query-syntax
将LINQ字符串解析为查询的最佳实践方法是什么?
换句话说,转换最有意义的方法是:
string query = @"from element in source
where element.Property = ""param""
select element";
Run Code Online (Sandbox Code Playgroud)
成
IEnumerable<Element> = from element in source
where element.Property = "param"
select element;
Run Code Online (Sandbox Code Playgroud)
假设source指的是IEnumerable<Element>或IQueryable<Element>在本地范围内.
从 .NET 4.6 开始,您可以使用 CSharpScript 来解析 Linq。假设您要解析的表达式在字符串变量“query”中,则执行以下操作:
string query = "from element in source where element.Property = ""param"" select element";
IEnumerable result = null;
try
{
var scriptOptions = ScriptOptions.Default.WithReferences(typeof(System.Linq.Enumerable).Assembly).WithImports("System.Linq");
result = await CSharpScript.EvaluateAsync<IEnumerable>(
query,
scriptOptions,
globals: global);
} catch (CompilationErrorException ex) {
//
}
Run Code Online (Sandbox Code Playgroud)
不要忘记传递您想要处理的(数据)源,并使用全局变量在脚本解析中访问它们。
它需要一些文本解析和大量使用System.Linq.Expressions。我在这里和这里做了一些玩弄。第二篇文章中的代码比第一篇有所更新,但仍有一些粗糙的地方。我偶尔会继续处理这个问题,并且有一个更简洁的版本,如果您有兴趣,我一直想发布它。我已经非常接近于支持 ANSI SQL 89 的一个很好的子集。
| 归档时间: |
|
| 查看次数: |
11250 次 |
| 最近记录: |