解析字符串的C#LINQ前pression [英] Parsing a string C# LINQ expression

查看:150
本文介绍了解析字符串的C#LINQ前pression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在这里做一些真正动态查询 - pferably $ P $没有在运行时,虽然调用编译器

I'm trying to do some really dynamic querying here - preferably without invoking the compiler at runtime though.

我有一个包含LINQ前pression,例如一个字符串。

I have a string containing a LINQ expression, e.g.

var s = "from a in queryable where a.Type == 1 select a";

我怎样才能从获得生成的IQueryable或Ex pressions?

How can I get the resulting IQueryable or Expressions from that?

我见过LINQPad和RavenDb都这样做,所以我相信有办法,我只是还没有找到它。

I've seen LINQPad and RavenDb both do this so I'm convinced there's a way, I just haven't found it yet.

推荐答案

您有一些选择:


  1. 做一些自产自销,解析文本,建设前pression树。这一标准的方法是使用一种语言解析器来解析字符串(如 ANTLR )。

使用codeDOM编译查询(不推荐用于生产environent,因为这是缓慢的,并产生每个编译,如果你做很多,这将饱和你的AppDomain与组件的装配。我要强调,不要如果您有什么样的量牛逼走这条路 - 虽然这是LINQPad做什么) -
<一href=\"http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6a4defd2-76f0-4865-97b7-130e4ba7b50a\">http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6a4defd2-76f0-4865-97b7-130e4ba7b50a

Use CodeDOM to compile the query (NOT recommended for a Production environent as this is slow and generates an assembly per compilation which will saturate your AppDomain with assemblies if you do many. Let me stress, don't go this route if you have any kind of volume - though this is what LINQPad does) - http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/6a4defd2-76f0-4865-97b7-130e4ba7b50a

使用Mono的编译器直接发出MSIL(所以每个编译没有装配和更快) - 的单声道编译器即服务(MCS)

Use Mono's compiler which emits MSIL directly (so no assembly per compilation and much faster) - Mono Compiler as a Service (MCS)

使用动态LINQ(有一定的局限性和限制,但基本上没有什么建议在点#1和很不错,重量轻,并具有只允许某些方法调用的能力。它解析文本查询,并建立从它的前pression树) - <一个href=\"http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx\">http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

Use Dynamic LINQ (has some limitations and restrictions, but basically does what is suggested in point #1 and is nice, lightweight, and has the ability to only allow certain method calls. It parses the text query and builds an Expression Tree from it) - http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx

这篇关于解析字符串的C#LINQ前pression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆