日期:2014-05-20  浏览次数:20994 次

一个关于linq优化的问题
代码如下
C# code

var pt = from d in product_DatadoDataContext.view_CategoryProgramProductTable
                         where d.CategoryProgramId == CategoryProgramId
                         select new ProductTableModel()
                         {
                             Sku = d.Sku,
                             ImageUrl = d.ImageUrl,
                             Url = d.Url,
                             ProductName = d.ProductName,
                             ProductDescription = d.ProductDescription,
                             ProductKeywords = Convert.ToString(d.ProductKeywords),
                             UserId = d.UserId,
                             BrandId = d.BrandId,
                             BrandsModel = (from b in product_DatadoDataContext.Brands
                                            where b.BrandId == d.BrandId
                                            select new BrandsModel()
                                            {
                                                BrandName = b.BrandName,
                                                Status = b.Status,
                                            }).FirstOrDefault(),
                             CreateDate = d.CreateDate,
                             UpdateDate = d.UpdateDate,
                             Status = d.Status,
                             Csku = d.Csku,
                             OptionsValuesList = (from pvl in product_DatadoDataContext.view_OptionsValuesProduct
                                                  where pvl.Sku.Equals(d.Sku)
                                                  select new OptionsValuesModel()
                                                  {
                                                      OId = pvl.OId,
                                                      OvId = pvl.OvId,
                                                      OptionsValue = pvl.OptionsValue,
                                                      Sort = Convert.ToInt32(pvl.Sort)
                                                  }).ToList(),
                             ProductCurrencyRelationList = (from pcr in product_DatadoDataContext.ProductCurrencyRelation
                                                            where pcr.Sku.Equals(d.Sku)
                                                            select new ProductCurrencyRelationModel()
                                                            {
                                                                CurrencyId = pcr.CurrencyId,
                                                                Sku = pcr.Sku,
                                                                Price = pcr.Price,
                                                                SpecialsPrice = pcr.SpecialsPrice
                                                            }).ToList()
                         };
                list = pt.ToList();


产品数据量比较大,一般都在1000左右
这样1000*3 查询数据3000次。
我本来想view_OptionsValuesProduct 先查出来,在内存二次检索。
view_OptionsValuesProduct 数据量太大,耗损太大。

求大神,给出减少查询次数,又不消耗大量内存的办法!!!!!

------解决方案--------------------
将业务逻辑封装在存储过程中进行调用
------解决方案--------------------
看着都蛋疼,全部放在内存里处理会死的,
------解决方案--------------------
探讨

看着都蛋疼


------解决方案--------------------
你的代码里用了FirstOrDefault ToList()这些操作符已经破坏了LINQ的延迟查询特性
虽然现在内存很便宜,但也不能这样做
------解决方案--------------------
存储过程吧