如何避免Entity Framework发出重复查询?

时间:2012-08-27 07:07:25

标签: linq entity-framework tsql

SQL profiler显示EF从以下LINQ查询中最多发出8次相同的查询:

var query = (from wtv in _context.WorkTypeVersions
               join rc in _context.RateCategories on wtv.WTVID equals rc.WTVID
               join rsc in _context.RateSubCategories on rc.RCID equals rsc.RCID
               where (wtv.Composite == (ckCompositePreambles.Checked ? "Y" : "N")
                      && rc.CatCode == catCode)

               select new pRateSubCategory()
                {
                  WTVID = wtv.WTVID,
                  RSCID = rsc.RSCID,
                  WTVersionName = wtv.WTVersionName,
                  SubCatCode = rsc.SubCatCode,
                  PCode = rsc.Preamble.SectionCode,
                  RateCategoryName = rc.RateCategoryName,
                  RateSubCategoryName = rsc.RateSubCategoryName,
                  FullSubCatName = rsc.FullSubCatName,
                  PMBID = rsc.PMBID,
                  Preamble = rsc.Preamble
                });

  dgvRateSubCategories.AutoGenerateColumns = false;
  lblSubSectionCount.Text = "SubSections: " + bsRateSubCategories.Count;
  dgvRateSubCategories.DataSource = bsRateSubCategories;

  bsRateSubCategories.DataSource = query;

TSQL查询(最多生成8次,但其他的是散布的)是:

    exec sp_executesql N'SELECT 
    [Project1].[WTVID] AS [WTVID], 
    [Project1].[RSCID] AS [RSCID], 
    [Project1].[WTVersionName] AS [WTVersionName], 
    [Project1].[SubCatCode] AS [SubCatCode], 
    [Project1].[SectionCode] AS [SectionCode], 
    [Project1].[RateCategoryName] AS [RateCategoryName], 
    [Project1].[RateSubCategoryName] AS [RateSubCategoryName], 
    [Project1].[FullSubCatName] AS [FullSubCatName], 
    [Project1].[PMBID] AS [PMBID], 
    [Project1].[PMBID1] AS [PMBID1], 
    [Project1].[PMB_Level] AS [PMB_Level], 
    [Project1].[PMB_Text] AS [PMB_Text], 
    [Project1].[Composite] AS [Composite], 
    [Project1].[PMB_XPS] AS [PMB_XPS]
    FROM ( SELECT 
        [Extent1].[WTVID] AS [WTVID], 
        [Extent1].[WTVersionName] AS [WTVersionName], 
        [Extent2].[RateCategoryName] AS [RateCategoryName], 
        [Extent3].[RSCID] AS [RSCID], 
        [Extent3].[PMBID] AS [PMBID], 
        [Extent3].[RateSubCategoryName] AS [RateSubCategoryName], 
        [Extent3].[SubCatCode] AS [SubCatCode], 
        [Extent3].[FullSubCatName] AS [FullSubCatName], 
        [Extent4].[PMBID] AS [PMBID1], 
        [Extent4].[PMB_Level] AS [PMB_Level], 
        [Extent4].[SectionCode] AS [SectionCode], 
        [Extent4].[Composite] AS [Composite], 
        [Extent4].[PMB_Text] AS [PMB_Text], 
        [Extent4].[PMB_XPS] AS [PMB_XPS]
        FROM    [dbo].[WorkTypeVersions] AS [Extent1]
        INNER JOIN [dbo].[RateCategories] AS [Extent2] ON [Extent1].[WTVID] = [Extent2].[WTVID]
        INNER JOIN [dbo].[RateSubCategories] AS [Extent3] ON [Extent2].[RCID] = [Extent3].[RCID]
        LEFT OUTER JOIN [dbo].[Preambles] AS [Extent4] ON [Extent3].[PMBID] = [Extent4].[PMBID]
        WHERE ([Extent1].[Composite] = (CASE WHEN (@p__linq__0 = 1) THEN N''Y'' ELSE N''N'' END)) AND ([Extent2].[CatCode] = @p__linq__1)
    )  AS [Project1]
    ORDER BY [Project1].[SubCatCode] ASC, [Project1].[WTVersionName] ASC',N'@p__linq__0 bit,@p__linq__1 varchar(8000)',@p__linq__0=0,@p__linq__1='A'    

EF的版本目前为:System.Data.Entity:4.0.0.0,EntityFramework:4.3.1

我在这里缺少什么?

1 个答案:

答案 0 :(得分:3)

问题出在您设置数据源的地方:

bsRateSubCategories.DataSource = query;

query变量实现了扩展IQueryable<T> interface DataSource所需的IEnumerable<T> interface ToList。当你将它绑定到一个网格时,网格将多次枚举结果以便自我渲染。

但是,因为您为网格提供了一个实现IQuerable<T>的数据源,每隔时间查询一次,它将对服务器执行查询,这可能是你多次见到它的原因。

为了缓解这种情况,您应该在将列表分配给DataSource之前实现列表,使用ToArray扩展方法,如此(如果它是只读的,{{3}}将是很好,但如果您需要添加新项目,请使用ToList):

bsRateSubCategories.DataSource = query.ToList();

当然,您必须决定何时重置DataSource,因为现在数据源完全与服务器断开连接。