簡體   English   中英

如何在高性能的實體框架上使用lambda表達式

[英]how use lambda expression on entity framework with high performance

我使用C#Winforms。 我使用實體框架6。

在我的解決方案中,我有2個具有A.BLL和A.DAL名稱的項目。 (在A.BLL參考上添加了A.DAL)

我在方法下的A.DAL項目:

public decimal Sum(Func<ml_doc, bool> predicate, Func<ml_doc, decimal> sumColumn)
{
    try
    {
        using (dbEnteties = new Entities(Connection.CustomEntityConnection))
        {
            dbEnteties.ContextOptions.LazyLoadingEnabled = true;
            var dbe = dbEnteties.ml_doc;
            return dbe.Where(predicate).Sum(sumColumn);
        }
    }
    catch (Exception exp)
    {
        throw exp;
    }
}

我在方法下使用A.BLL項目(在A.DAL項目上使用Sum方法):

public decimal GetSum(long AccId, int? CntId, short BranchId, int BeginNumber)
{
    try
    {
        using (dal = new DAL.DocDA())
        {
            // Sum method referenced from A.DAL project
            return dal.Sum(
                x =>
                x.acc_id == AccId &&
                x.cnt_id.Equals(CntId) &&
                x.ml_doc_hdr.branch_id == BranchId &&
                x.ml_doc_hdr.number >= BeginNumber
                ,
                y => y.price);
        }
    }
    catch (Exception exp)
    {
        throw exp;
    }
}

當我使用GetSum方法(在A.BLL項目中)時,出現以下異常:

已經有一個與此命令相關聯的打開的DataReader,必須先關閉它。

為了解決此異常,我將MultipleActiveResultSets = True添加到我的連接字符串中,此方法的運行速度非常慢(例如3秒)。

我在A.DAL項目上的方法下創建:

public decimal Sum2(long AccId, int? CntId, short BranchId, int BeginNumber)
{
    try
    {
        using (dbEnteties = new Entities(Connection.CustomEntityConnection))
        {
            dbEnteties.ContextOptions.LazyLoadingEnabled = true;
            var resultQuery = dbEnteties.ml_doc.Where(
                x =>
                x.acc_id == AccId &&
                x.cnt_id.Equals(CntId) &&
                x.ml_doc_hdr.branch_id == BranchId &&
                x.ml_doc_hdr.number >= BeginNumber
                );

            if (resultQuery.Count() != 0)
            {
                return resultQuery.Sum(x => x.price);
            }

            return 0;
        }
    }
    catch (Exception exp)
    {
        throw exp;
    }
}

當我使用上層方法(Sum2)時,此工作很好且非常快(例如0.003秒)

Sum2(在A.DAL項目中)和GetSum(在A.BLL projetc中)方法(似乎有相同)之間的區別是什么?

如何更改GetSum方法以實現高性能?

這個:

public decimal Sum(Func<ml_doc, bool> predicate, Func<ml_doc, decimal> sumColumn)
{
    try
    {
        using (dbEnteties = new Entities(Connection.CustomEntityConnection))
        {
            dbEnteties.ContextOptions.LazyLoadingEnabled = true;
            var dbe = dbEnteties.ml_doc;
            return dbe.Where(predicate).Sum(sumColumn);
        }
    }
    catch (Exception exp)
    {
        throw exp;
    }
}

從SQL Server本地加載完整的ml_doc表,然后在本地執行Where()Sum()操作。

這是因為您的LINQ表達式使用兩個Func<>委托,所以不要使用

它用

嘗試將其更改為:

public decimal Sum(Expression<Func<ml_doc, bool>> predicate, Expression<Func<ml_doc, decimal>> sumColumn)

相反,通過直接使用一些lambda函數, Sum2方法使用Queryable.*方法,因為lambda函數被解釋為Expression<>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM