Linq编译查询和性能问题

发布于 2024-12-29 08:28:39 字数 4561 浏览 1 评论 0原文

我遇到 linq 编译查询的一些性能问题。

using (var txn = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))
        {
            DateTime dealcheck = new DateTime(1753, 2, 2);

            Func<DealDataClassesDataContext, string, IQueryable<DealsDetails>> DD =
            CompiledQuery.Compile<DealDataClassesDataContext, string, IQueryable<DealsDetails>>

    ((DealDataClassesDataContext nw, string sCity) =>

        from D in nw.Deals

        where D.Address == City && (D.DealTime >= DateTime.Now || D.DealTime == dealcheck) && PriceMax >= D.DealPrice && D.DealPrice >= PriceMin && DisCountMax >= D.SavingsRate && D.SavingsRate >= DiscountMin && (D.DealTime >= DateTime.Now.AddDays(TimeMin) && D.DealTime <= DateTime.Now.AddDays(TimeMax) || D.DealTime == dealcheck)

        select new DealsDetails(
                          lst,
                          D.DealId,
                          D.DealHeadline,
                          D.DealCategory,
                          D.BuyPrice,
                          D.DealPrice,
                          D.SavingsRate,
                          D.SavingAmount,
                          D.RelatedWebsite,
                          D.Address,
                          string.Empty,
                          D.DealImage,
                          string.Empty,
                          string.Empty,
                          D.Time, D.CurrentTime, D.DealTime,
                         D.Location, string.Empty, string.Empty, D.Latitude, D.Longitude, D.Islocal, D.VendorMail, D.MerchantInfo, D.Review, D.HowItWork, D.DealUrl
                          ));

            string jString = "";

            //int a = q(DealDbContext1, "London").Count();

            using (DealDataClassesDataContext db = new DealDataClassesDataContext())
            {
                IQueryable<DealsDetails> DDD = DD.Invoke(DealDbContext, "London");

                if (lstSite.Count > 0 && lstSite[0] != "AllDeals")
                {
                    DDD = DDD.Where(D => D.RelatedWebsite.Split(',').Where(x => lstSite.Contains(x)).Any()); //.Where(row => row.Category.ToList().Where(x => lst.Contains(x)).Any()).ToList();
                }
                if (lst.Count > 0)
                {
                    DDD = DDD.Where(D => D.Categories.Split(',').Where(x => lst.Contains(x)).Any()); //.Where(row => row.Category.ToList().Where(x => lst.Contains(x)).Any()).ToList();
                }
                if (sortby == "Time" && orderby == "Asc")
                {
                    DDD = (from d in DDD orderby d.Time ascending select d).Skip((Page - 1) * PageSize).Take(PageSize);
                }
                else if (sortby == "Time" && orderby == "Desc")
                {
                    DDD = (from d in DDD orderby d.Time descending select d).Skip((Page - 1) * PageSize).Take(PageSize);
                }
                else if (sortby == "Price" && orderby == "Asc")
                {
                    DDD = (from d in DDD orderby d.DealPrice ascending select d).Skip((Page - 1) * PageSize).Take(PageSize);
                }
                else if (sortby == "Price" && orderby == "Desc")
                {
                    DDD = (from d in DDD orderby d.DealPrice descending select d).Skip((Page - 1) * PageSize).Take(PageSize);
                }
                else if (sortby == "Percent" && orderby == "Asc")
                {
                    DDD = (from d in DDD orderby d.SavingsRate ascending select d).Skip((Page - 1) * PageSize).Take(PageSize);
                }
                else if (sortby == "Percent" && orderby == "Desc")
                {
                    DDD = (from d in DDD orderby d.SavingsRate descending select d).Skip((Page - 1) * PageSize).Take(PageSize);
                }
                else
                {
                    DDD = DDD.Skip((Page - 1) * PageSize).Take(PageSize);
                }

                string Currency = "$";
                foreach (DealsDetails item in DDD)
                {
                    //Creating Html String Here
                }

                return jString;

我已在此处附加了我的整个代码,请检查这有什么问题,它需要很长时间才能响应大约 20 秒。

主要是 foreach 循环花费了超过 17 秒。

请让我知道如何编译此查询。

提前致谢。

I am having some performance issue of linq compiled query.

using (var txn = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }))
        {
            DateTime dealcheck = new DateTime(1753, 2, 2);

            Func<DealDataClassesDataContext, string, IQueryable<DealsDetails>> DD =
            CompiledQuery.Compile<DealDataClassesDataContext, string, IQueryable<DealsDetails>>

    ((DealDataClassesDataContext nw, string sCity) =>

        from D in nw.Deals

        where D.Address == City && (D.DealTime >= DateTime.Now || D.DealTime == dealcheck) && PriceMax >= D.DealPrice && D.DealPrice >= PriceMin && DisCountMax >= D.SavingsRate && D.SavingsRate >= DiscountMin && (D.DealTime >= DateTime.Now.AddDays(TimeMin) && D.DealTime <= DateTime.Now.AddDays(TimeMax) || D.DealTime == dealcheck)

        select new DealsDetails(
                          lst,
                          D.DealId,
                          D.DealHeadline,
                          D.DealCategory,
                          D.BuyPrice,
                          D.DealPrice,
                          D.SavingsRate,
                          D.SavingAmount,
                          D.RelatedWebsite,
                          D.Address,
                          string.Empty,
                          D.DealImage,
                          string.Empty,
                          string.Empty,
                          D.Time, D.CurrentTime, D.DealTime,
                         D.Location, string.Empty, string.Empty, D.Latitude, D.Longitude, D.Islocal, D.VendorMail, D.MerchantInfo, D.Review, D.HowItWork, D.DealUrl
                          ));

            string jString = "";

            //int a = q(DealDbContext1, "London").Count();

            using (DealDataClassesDataContext db = new DealDataClassesDataContext())
            {
                IQueryable<DealsDetails> DDD = DD.Invoke(DealDbContext, "London");

                if (lstSite.Count > 0 && lstSite[0] != "AllDeals")
                {
                    DDD = DDD.Where(D => D.RelatedWebsite.Split(',').Where(x => lstSite.Contains(x)).Any()); //.Where(row => row.Category.ToList().Where(x => lst.Contains(x)).Any()).ToList();
                }
                if (lst.Count > 0)
                {
                    DDD = DDD.Where(D => D.Categories.Split(',').Where(x => lst.Contains(x)).Any()); //.Where(row => row.Category.ToList().Where(x => lst.Contains(x)).Any()).ToList();
                }
                if (sortby == "Time" && orderby == "Asc")
                {
                    DDD = (from d in DDD orderby d.Time ascending select d).Skip((Page - 1) * PageSize).Take(PageSize);
                }
                else if (sortby == "Time" && orderby == "Desc")
                {
                    DDD = (from d in DDD orderby d.Time descending select d).Skip((Page - 1) * PageSize).Take(PageSize);
                }
                else if (sortby == "Price" && orderby == "Asc")
                {
                    DDD = (from d in DDD orderby d.DealPrice ascending select d).Skip((Page - 1) * PageSize).Take(PageSize);
                }
                else if (sortby == "Price" && orderby == "Desc")
                {
                    DDD = (from d in DDD orderby d.DealPrice descending select d).Skip((Page - 1) * PageSize).Take(PageSize);
                }
                else if (sortby == "Percent" && orderby == "Asc")
                {
                    DDD = (from d in DDD orderby d.SavingsRate ascending select d).Skip((Page - 1) * PageSize).Take(PageSize);
                }
                else if (sortby == "Percent" && orderby == "Desc")
                {
                    DDD = (from d in DDD orderby d.SavingsRate descending select d).Skip((Page - 1) * PageSize).Take(PageSize);
                }
                else
                {
                    DDD = DDD.Skip((Page - 1) * PageSize).Take(PageSize);
                }

                string Currency = "$";
                foreach (DealsDetails item in DDD)
                {
                    //Creating Html String Here
                }

                return jString;

I have attached My whole code here please check what is the issue with this it's taking too long time to respond arround 20 sec.

Mainly the foreach loop takng more then 17 sec.

Please let me know how do I compile this query.

Thanks in Advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日久见人心 2025-01-05 08:28:39

除了预编译查询之外,您还应该预生成本地查询视图< /a>

In addition to pre-compiling your query, you should pre-generate the local query views

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文