标签: compiled-query

编译LINQ查询和DataLoadOptions ...扭曲!

我知道这里讨论的方法:

解决Linq to Sql中针对高需求ASP.NET网站的编译查询的常见问题

...但这对我的情况不起作用,因为我得到了:

"从查询返回结果后,不允许设置加载选项."

我使用Codesmith PLINQO脚本生成实体和管理器代码,管理器代码如下所示:

public partial class SearchManager
{       
    #region Query
    // A private class for lazy loading static compiled queries.
    private static partial class Query
    {
        internal static readonly Func<MyDataContext,IOrderedQueryable<Search>> 
            GetAll = CompiledQuery.Compile(
                (MyDataContext db) =>
                from s in db.Search
                orderby s.Name
                select s);
    } 
    #endregion


    public IQueryable<Search> GetAll()
    {
        return Query.GetAll(Context);
    }
}
Run Code Online (Sandbox Code Playgroud)

我首先尝试将静态DataLoadOptions拖放到Searchmanager类中,如下所示:

public static readonly DataLoadOptions MyOptions = 
    (new Func<DataLoadOptions>(() =>
    {
        var option = new DataLoadOptions();
        option.LoadWith<Search>(x => x.Rule);
        return option; …
Run Code Online (Sandbox Code Playgroud)

c# linq loadoptions compiled-query

6
推荐指数
1
解决办法
2577
查看次数

包括编译查询中的"离线"代码

当我在我的编译查询中包含一个函数时,窗帘后面会发生什么,就像我在这里使用DataConvert.ToThema()将表对象转换为我的自定义业务对象:

public static class Queries
{
    public static Func<MyDataContext, string, Thema> GetThemaByTitle
    {
        get
        {
            var func = CompiledQuery.Compile(
                (MyDataContext db, string title) =>
                    (from th in elan.tbl_Thema
                     where th.Titel == title
                     select DataConvert.ToThema(th)).Single()
                     );
            return func;
        }
    }
}

public static class DataConvert
{
    public static Thema ToThema(tbl_Thema tblThema)
    {
        Thema thema = new Thema();

        thema.ID = tblThema.ThemaID;
        thema.Titel = tblThema.Titel;
        // and some other stuff

        return thema;
    }
}
Run Code Online (Sandbox Code Playgroud)

并称之为这样

Thema th = Queries.GetThemaByTitle.Invoke(db, "someTitle");
Run Code Online (Sandbox Code Playgroud)

显然,该函数没有转换为SQL或其他东西(怎么可能),但是当我在VS2010中设置断点时它也不成立.

它没有问题,但我不明白为什么或为什么.究竟发生了什么?

.net c# linq-to-sql compiled-query

6
推荐指数
1
解决办法
215
查看次数

CompiledQuery抛出ArgumentException

我试图在扩展方法中使用一个非常简单的预编译查询来使用LINQ to SQL检索数据.SytelineRepository是我使用外部映射文件的自定义DataContext,我正在查询JobOrders表.我一直在使用自定义DataContext很长一段时间没有问题,执行相同的查询没有问题; 我只是想提高性能.但是,当我尝试使用CompiledQuery.Compile时,我得到下面的ArgumentException.

这是代码:

public static class Queries
{
    public static readonly Func<SytelineRepository, String, IQueryable<JobOrder>> GetOpenJobOrdersForItemQuery =
        CompiledQuery.Compile(
        (SytelineRepository r, String itemNumber) => 
            from job in r.JobOrders
            where job.ItemNumber == itemNumber
            select job
        );

    public static IQueryable<JobOrder> GetOpenJobOrdersForItem(this SytelineRepository r, System.String itemNumber)
    {
        return GetOpenJobOrdersForItemQuery(r, itemNumber);
    }

}
Run Code Online (Sandbox Code Playgroud)

相比之下,这有效:

    public static IEnumerable<JobOrder> GetOpenJobOrdersForItem(this SytelineRepository r, System.String itemNumber)
    {
        return GetOpenJobOrdersForItemUncompiledQuery(r, itemNumber);
    }

    public static IQueryable<JobOrder> GetOpenJobOrdersForItemUncompiledQuery(SytelineRepository r, String itemNumber)
    {
        return
            from job in r.JobOrders
            where job.ItemNumber == itemNumber …
Run Code Online (Sandbox Code Playgroud)

.net c# linq-to-sql compiled-query

5
推荐指数
1
解决办法
640
查看次数

为什么CompiledQuery没有提高性能?

我试图加快一个经常使用的查询.使用a CompiledQuery似乎是答案.但是当我尝试编译版本时,编译版和非编译版之间的性能没有差异.

有人可以告诉我为什么使用Queries.FindTradeByTradeTagCompiled不比使用更快Queries.FindTradeByTradeTag

static class Queries
{
    // Pre-compiled query, as per http://msdn.microsoft.com/en-us/library/bb896297
    private static readonly Func<MyEntities, int, IQueryable<Trade>> mCompiledFindTradeQuery =
        CompiledQuery.Compile<MyEntities, int, IQueryable<Trade>>(
            (entities, tag) => from trade in entities.TradeSet
                               where trade.trade_tag == tag
                               select trade);

    public static Trade FindTradeByTradeTagCompiled(MyEntities entities, int tag)
    {
        IQueryable<Trade> tradeQuery = mCompiledFindTradeQuery(entities, tag);

        return tradeQuery.FirstOrDefault();
    }

    public static Trade FindTradeByTradeTag(MyEntities entities, int tag)
    {
        IQueryable<Trade> tradeQuery = from trade in entities.TradeSet
                                       where trade.trade_tag == tag
                                       select trade;

        return …
Run Code Online (Sandbox Code Playgroud)

c# performance entity-framework compiled-query

3
推荐指数
2
解决办法
2241
查看次数

哪种方法可以关闭已编译的查询

正如我们所知,您无法在编译查询中添加.Where()或使用额外的子句.First(),因为这会更改查询并强制重新编译.我想知道的是哪些方法可用于"关闭"编译的查询.

我知道大多数人都使用.AsEnumerable()或者.ToList(),但其他哪些方法也可以使用?我可以使用.AsQueryable(),或者这是一个无操作?

哪个性能更好?我知道.AsEnumerable()速度比快.ToList(),但如果我想要一个IQueryable,那.AsEnumerable().AsQueryable()比它.ToList()好吗?

.net linq entity-framework compiled-query

3
推荐指数
1
解决办法
1177
查看次数

从Linq编译的查询返回KeyValuePair

我刚刚接触到Linq中的Compiled Queries,并且遇到了一些奇怪的行为.

此查询编译正常:

public static Func<DataContext, string, object> GetJourneyByUrl =
    CompiledQuery.Compile<DataContext, string, object>((DataContext dc, string urlSlug) =>
        from j in dc.Journeys
        where !j.Deleted
        where j.URLSlug.Equals(urlSlug)
        select new KeyValuePair<string, int>(j.URLSlug, j.JourneyId)
    );
Run Code Online (Sandbox Code Playgroud)

但是当我尝试将返回类型从对象更改为KeyValuePair时,如下所示:

public static Func<DataContext, string, KeyValuePair<string, int>> GetJourneyByUrl =
    CompiledQuery.Compile<DataContext, string, KeyValuePair<string, int>>((DataContext dc, string urlSlug) =>
        from j in dc.Journeys
        where !j.Deleted
        where j.URLSlug.Equals(urlSlug)
        select new KeyValuePair<string, int>(j.URLSlug, j.JourneyId)
    );
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

CS1662:无法将lambda表达式转换为委托类型'System.Func <DataContext,string,System.Collections.Generic.KeyValuePair <string,int >>',因为块中的某些返回类型不能隐式转换为委托返回类型

如何KeyValuePair从编译的查询中返回单个?或者我完全以错误的方式解决这个问题?

c# linq compiled-query keyvaluepair

3
推荐指数
1
解决办法
1563
查看次数