Asp.net Linq编译查询和性能问题

Asp.net Linq编译查询和性能问题,asp.net,linq-to-sql,compiled-query,Asp.net,Linq To Sql,Compiled Query,我有一些linq编译查询的性能问题 using (var txn = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted })) { DateTime dealcheck = new DateTime(1753, 2,

我有一些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;
使用(var txn=newtransactionscope(TransactionScopeOption.Required,newtransactionoptions{IsolationLevel=System.Transactions.IsolationLevel.ReadUncommitted}))
{
DateTime dealcheck=新的日期时间(1753,2,2);
Func DD=
CompiledQuery.Compile
((DealDataClassesDataContext nw,字符串sCity)=>
来自西北部的D区。成交
其中D.Address==City&&(D.DealTime>=DateTime.Now | | D.DealTime==dealcheck)&&PriceMax>=D.DealPrice&&D.DealPrice>=PriceMin&&DealTempMax>=D.SavingsRate&&D.SavingsRate>=DeatTime.Now.AddDays(TimeMin)&&D.DealTime 0&&lstSite[0]!=“所有交易”)
{
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();
}
如果(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();
}
如果(排序依据=“时间”和排序依据=“Asc”)
{
DDD=(从DDD顺序中的d开始,按d。时间升序选择d)。跳过((第1页)*页面大小)。获取(页面大小);
}
else if(排序方式==“时间”和排序方式==“描述”)
{
DDD=(从DDD中的d按d排序。时间递减选择d)。跳过((第1页)*页面大小)。获取(页面大小);
}
否则,如果(排序依据==“价格”&排序依据==“Asc”)
{
DDD=(从DDD orderby d.DealPrice中的d开始选择d)。跳过((第1页)*页面大小)。获取(页面大小);
}
否则,如果(排序方式==“价格”&排序方式==“描述”)
{
DDD=(从DDD orderby d.DealPrice中的d开始,选择d)。跳过((第1页)*页面大小)。获取(页面大小);
}
否则如果(排序依据=“百分比”&排序依据=“Asc”)
{
DDD=(从DDD orderby d.SavingsRate升序选择d中的d)。跳过((第1页)*页面大小)。获取(页面大小);
}
否则如果(排序依据=“百分比”&排序依据=“描述”)
{
DDD=(从DDD中的d按d.SavingsRate降序选择d)。跳过((第1页)*页面大小)。获取(页面大小);
}
其他的
{
DDD=DDD.Skip((第1页)*页面大小)。Take(页面大小);
}
字符串Currency=“$”;
foreach(DDD中的DealsDetails项目)
{
//在这里创建Html字符串
}
返回jString;
我已经在这里附上了我的全部代码,请检查这是什么问题,它花了太长的时间在20秒左右作出回应

主要是foreach循环耗时超过17秒

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


提前感谢。

除了预编译您的查询外,您应该

您的问题没有足够的详细信息您已经尝试解决了什么问题,但我真诚地怀疑20秒与Linq部分有任何关系。我建议您使用SQL manager中的探查器检查实际传递到数据库的查询是什么如果对数据库手动运行,可能不会运行得更快。