经常和编译的LINQ的等价到SQL? [英] Equivalence between regular and compiled Linq to SQL?

查看:77
本文介绍了经常和编译的LINQ的等价到SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在努力改变一些现有的LINQ to SQL的成编译查询部分使用的有价值的文章作为指导

I'm working on transforming some existing Linq to SQL into Compiled queries, in part using this helpful article as a guide.

下面是我原来的语句的一个例子:

Below is an example of one of my original statements:

    private IQueryable<Widget> GetWidgetQuery()
    {
        return db.Widgets.Where(u => (!u.SomeField.HasValue || !u.SomeField.Value));
    }

下面是我在创造一个编译的查询尝试:

Here's my attempt at creating a compiled query:

    private static readonly Func<DBDataContext, IQueryable<Widget>> GetWidgetQuery = 
        CompiledQuery.Compile((DBDataContext db) => 
        db.Widgets.Where(u => (!u.SomeField.HasValue || !u.SomeField.Value)));



我有一些难以想像的标准,此查询的编译化身之间的差异。假设我的语法得当,将已编译的查询返回相同的数据作为标准之一,只是使用编译查询提供的优势是什么?

I'm having some trouble visualizing the differences between the standard and compiled incarnations of this query. Assuming my syntax is proper, will the compiled query return the same data as the standard one, just with the advantages using Compiled queries provides?

推荐答案

是的,它会返回相同的数据 - 的的IQueryable<窗口小部件>对象 - 但第一个例子不同的是,如果你进一步扩展查询你会失去已编译的查询的好处。

Yes it will return the same data - the IQueryable<Widget> object - but unlike the first example, you'll lose the benefits of the compiled query if you extend the query further.

您需要,当你调用GetWidgetQuery()来传递DBDataContext对象。

You will need to pass the DBDataContext object when you call GetWidgetQuery().

DBDataContext db;



返回的IQueryable<窗口小部件>

Returns IQueryable<Widget>:

var widgets = GetWidgetQuery(db);

使用LINQ到SQL,这通过对结果执行LINQ查询失去编译查询的好处

With LINQ to SQL, this loses the benefit of the compiled query by performing a LINQ query on the results:

var widgetsUncompiled = GetWidgetQuery(db).Where(u => u.SomeField.HasValue); 

这篇关于经常和编译的LINQ的等价到SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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