【问题标题】:Enumerate Linq Call takes 5 minutes枚举 Linq 调用需要 5 分钟
【发布时间】:2018-06-11 14:36:25
【问题描述】:

我在使用此代码时遇到了性能问题, 我正在从数据库中读取数据,然后将 DataTable 转换为 CampaignRecipientLib 列表,调用此方法至少需要 5 分钟:

private static List<CampaignRecipientLib> GetGroupRcipientsToSchedule(int GroupId, int CustomerId, bool Delete = false, bool NewlyAddedCampaignGroupRecipient = false)
        {
            List<SqlParameter> param = new List<SqlParameter>();
            param.Add(new SqlParameter("@GroupID", GroupId));
            param.Add(new SqlParameter("@CustomerId", CustomerId));
            param.Add(new SqlParameter("@Delete", Delete));
            param.Add(new SqlParameter("@NewlyAddedCampaignGroupRecipient", NewlyAddedCampaignGroupRecipient));

            Stopwatch st = new Stopwatch();
            st.Start();
            var rec_names = SqlHelper.GetDataTable("[dbo].[p_GetRecipientListByGroupCode]", param, CommandType.StoredProcedure);
            st.Stop();
            return rec_names.AsEnumerable().Select(row =>
                 new CampaignRecipientLib
                 {
                     ID = row.Field<int>("cgr_RecipientId"),
                     CMPRID = 0,//row.Field<int>("cr_CampaignRecipientId"),
                     CMPRCode = codePrefix + Guid.NewGuid().ToString("N"),//row.Field<string>("cr_CampaignRecipientCode"),
                     CampaignId = 0,
                     SentCampaigns = string.IsNullOrEmpty(row.Field<string>("GroupScenarioList")) ? new List<int>() : row.Field<string>("GroupScenarioList").Split(',').Select(int.Parse).ToList(),
                     EmailTo = row.Field<string>("r_Email"),
                     Email = row.Field<string>("r_Email"),
                     FirstName = row.Field<string>("r_FirstName"),
                     LastName = row.Field<string>("r_LastName"),
                     Language = string.IsNullOrEmpty(row.Field<string>("r_LangCode")) ? null : row.Field<string>("r_LangCode"),
                     Scheduled = DateTime.Now,//row.Field<DateTime?>("cr_Scheduled").HasValue ? row.Field<DateTime?>("cr_Scheduled") : DateTime.Now,
                     IsdoubleBarrle = false,//row.Field<bool>("cr_IsDoubleBarrel"),
                     Offset = string.IsNullOrEmpty(row.Field<string>("r_Offset")) ? null : row.Field<string>("r_Offset"),
                     Toffset = row.Field<string>("r_Offset").StartsWith("-") ? -TimeSpan.Parse(row.Field<string>("r_Offset").Remove(0, 1)) : TimeSpan.Parse(row.Field<string>("r_Offset")),
                     ReadyTobeSent = false,//row.Field<bool>("cr_ReadyTobeSent"),
                     PickupReady = false,//row.Field<bool>("cr_PickupReady"),
                     DefaultLanguage = string.Empty//string.IsNullOrEmpty(row.Field<string>("cmp_LangCode")) ? null : row.Field<string>("cmp_LangCode")
                 }
                 ).ToList();
        }

同样的调用,在不同的项目中超级快,不到一秒! 我的意思是不同的项目是:windows服务,而当前需要较长时间的项目是:Asp Web Form application

有什么线索或建议吗?

注意:调用DataBase需要1.5秒才能返回一个有4000行的DataTable,但枚举部分是耗时最多的部分

return rec_names.AsEnumerable().Select(row =>
                 new CampaignRecipientLib
                 { .... }).ToList();

===========================

更新:

这是系统 A 慢的截图:

标记的线可能是原因吗?因为我正在将该列的内容转换为逗号分隔的 int 值

这是系统 B 的截图,它的速度超级快,但我不需要在此调用中绑定 GroupScenarioList 列

【问题讨论】:

    标签: c# linq datatable enumerate


    【解决方案1】:

    我找到了原因

    codePrefix是一个属性,每次对dataTable中的每一行都调用一个存储过程,

     return rec_names.AsEnumerable().Select(row =>
                     new CampaignRecipientLib
                     {
                         ID = row.Field<int>("cgr_RecipientId"),
                         CMPRID = 0,//row.Field<int>("cr_CampaignRecipientId"),
                         CMPRCode = codePrefix + Guid.NewGuid().ToString("N"),//row.Field<string>("cr_CampaignRecipientCode"),
                         CampaignId = 0,
                         ... ....
    

    我只是通过将“codePrefix”属性返回的唯一值存储在局部变量中来将其更改为调用一次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      • 2018-07-20
      • 1970-01-01
      • 2018-03-03
      • 2016-04-03
      • 2020-10-19
      相关资源
      最近更新 更多