简体   繁体   English

亚音速-将linq查询转换为sql查询/ DataReader

[英]subsonic - convert an linq query to sql query / DataReader

Let's say I have a the following query: 假设我有以下查询:

int x = 5;
var result = from p in db.products
             where p.CategoryId == x
             select p;

int count = result.Count();
List<product> products = result.ToList();

That's what I have now. 那就是我现在所拥有的。 But aditionally I need to have a DataReader from result: 但是,另外我需要从结果中获取一个DataReader:

// that's what I need:
var reader = ConvertSubSonicLinqQueryToDataReader(result);

How can I convert the linq statement to something I can work with? 如何将linq语句转换为可以处理的内容? A DataReader or a DbCommand or even plain sql with a list of paramters. 带有参数列表的DataReader或DbCommand甚至纯SQL。

I know SubSonic can do that (since it translates the query to plain sql anyway) but I haven't found anything in the public accessible methods yet. 我知道SubSonic可以做到这一点(因为无论如何它都会将查询转换为纯sql),但是我尚未在公共可访问方法中找到任何东西。

Any suggestions? 有什么建议么?

Converting the LINQ query is the wrong approach. 转换LINQ查询是错误的方法。 LINQ returns results at a level of abstraction higher than a DataReader works at. LINQ以比DataReader更高的抽象级别返回结果。

There's also the issue of deferred execution so your LINQ query may not be executed as a single SQL statement anyway. 还有延迟执行的问题,因此您的LINQ查询可能始终无法作为单个SQL语句执行。

Rater than use a LINQ statement why not just use an SqlQuery instead? 比使用LINQ语句SqlQuery为什么不只使用SqlQuery代替呢?

var qry = new Select().From(Product.Schema).Where(Product.CategoryIdColumn).IsEqualTo(x);

return qry.ExecuteReader();

Edit: 编辑:
Just seen you're using SubSonic3 (not 2 as the above code would be for) but the potential misuse of LINQ and duplication of work still stands. 刚刚看到您使用的是SubSonic3(上面的代码不是2),但是仍然存在LINQ的滥用和重复工作的情况。

The code that creates object from the DataReader can be found in DbDataProvider.ToEnumerable. 在DbDataProvider.ToEnumerable中可以找到从DataReader创建对象的代码。 It's called from DbQueryProvider's Execute method (line 227). 从DbQueryProvider的Execute方法调用(第227行)。 The best way to "understand" the LINQ magic is to place some breakpoints on DbQueryProvider methods. “了解” LINQ魔术的最佳方法是在DbQueryProvider方法上放置一些断点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM