Linq2Sql-为将来的动态执行存储复杂的Linq查询-原始文本-可能吗? [英] Linq2Sql - Storing Complex Linq Queries for future dynamic execuction - raw text - possible?

查看:68
本文介绍了Linq2Sql-为将来的动态执行存储复杂的Linq查询-原始文本-可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Linq2Sql让我很开心.表达式树很棒,仅标准的Linq2Sql语法就很有趣.

I am having a lot of fun with Linq2Sql. Expression Trees have been great, and just the standard Linq2Sql syntax has been a lot of fun.

现在我进入了应用程序的一部分,我必须以某种方式将查询存储在数据库中,这些查询是针对使用相同数据库和相同表(嗯,视图,但您知道我的意思)的不同客户定制的.基本上,我无法对任何内容进行硬编码,并且必须保留查询语言的明文形式,以便有人可以编写新的从句类型"查询.

I am now down to part of my application where I have to somehow store queries in a database, that are custom for different customers that use the same database and same tables (well, view, but you know what I mean). Basically, I cant hard-code anything, and I have to leave the query language clear text so someone can write a new where-clause type query.

因此,如果描述过于苛刻,让我澄清一下:

So, if that description was harsh, let me clarify:

在我们应用程序的先前版本中,我们曾经使用原始SQL将SQL直接调用到数据库.是的.很有趣,很脏,而且很有效.我们将拥有一个充满不同条件的数据库表,例如

In a previous version of our application, we used to do direct SQL calls to the db using raw SQL. Yea. it was fun, dirty, and it worked. We would have a database table fulled of different criteria like

(EventType = 6 and Total > 0)

或子查询样式

(EventType = 7 
AND Exists (
    select * 
    from events as e1 
        where events.EventType = e1.EventType 
        and e1.objectNumber = 89)
    )

(SQL注入有人吗?)

(sql injection anyone?)

在Linq2Sql中,这更具挑战性.我可以使所有这些查询在CLR中都没问题,但是能够将条件动态传递给Linq更具挑战性,尤其是在我想执行子查询的情况下(如上述示例).

In Linq2Sql, this is a little more challenging. I can make all these queries no problem in the CLR, but being able to pass dynamic where criterias to Linq is a little more challenging, especially if I want to perform a sub query (like the above example).

我有一些想法:

获取原始表达式并将其存储---但我不知道如何获取原始文本表达式并将其反向转换为可执行的对象表达式.

Get the raw expression, and store it --- but I have no idea how to take the raw text expression and reverse it back to executable to object expression.

编写类似于SQl的语言,并让其解析代码并生成Linq Expression-哇,这可能很有趣

Write a SQl like language, and have it parse the code and generate Linq Expression -- wow, that could be a lot of fun

我很确定没有SomeIqueryable.Where("EventType = 6 and Total > 54").我在阅读它在beta1中可用的信息,但是我现在不知道如何实现.

I am quite sure there is no SomeIqueryable.Where("EventType = 6 and Total > 54"). I was reading that it was available in beta1, but I don't see how you can do that now.

var exp2 = context.POSDataEventView.Where("EmployeeNumber == @0", 8310); 

这是我部署的最简单方法.我认为.

This would be the easiest way for me to deploy.. I think.

存储序列化的表达式-哇,这会让试图编写查询的用户感到困惑---地狱,我不确定我什至不能全部输入.

Store serialized Expressions -- wow.. that would be confusing to a user trying to write a query --- hell, I'm not sure I could even type it all out.

因此,我正在寻找一些有关如何以某种明文形式存储查询,然后以某种方式针对我的Linq2Sql对象执行查询而不调用ExecuteSQL的想法.我想使用LinqObjects.

So, I am looking for some ideas on how I can store a query in some kind of clear text, and then execute it against my Linq2Sql objects in some fashion without calling the ExecuteSQL. I want to use the LinqObjects.

P.S.如果有帮助,我正在为此应用程序使用pLinqo.它仍然是linq2sql.

P.S. I am using pLinqo for this application if that helps. Its still linq2sql though.

提前谢谢!

推荐答案

也许

Perhaps the Dynamic LINQ Library (in the MSDN samples) would help?

尤其是用法如下:

这可以与任何IQueryable<T>源一起使用-包括LINQ-to-Objects,只需在序列上调用.AsQueryable()(通常为IEnumerable<T>)即可.

This should work with any IQueryable<T> source - including LINQ-to-Objects simply by calling .AsQueryable() on the sequence (typically IEnumerable<T>).

这篇关于Linq2Sql-为将来的动态执行存储复杂的Linq查询-原始文本-可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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