【问题标题】:Big Entity Framework Query, How to pregenerate, or precompile this query大实体框架查询,如何预生成或预编译此查询
【发布时间】:2011-12-21 07:38:06
【问题描述】:

我有一个大的 Linq-to-entity 查询,将 linq 查询转换为 SQL 似乎很长。

如何预编译此查询。 有没有办法做到这一点。


这是我的查询。

 var ListLocation = from s in repLocation.GetLocationByPermision()
                                   .Where(p => (ViewModel.BaseSearchViewModel.IsActive == -1 || p.IsActive == bIsActive) &&
                                                    (ViewModel.LocationTypeID < 0 || ViewModel.LocationTypeID == p.LocationTypeID) &&

                                                    (ViewModel.ListUsageID.Count() == 0 || p.Premises.Select(gs => gs.UsageID).Intersect(ViewModel.ListUsageID).Any()) &&
                                                    (ViewModel.BaseSearchViewModel.City == null || p.Address.City.CityName.Contains(ViewModel.BaseSearchViewModel.City)) &&
                                                    (ViewModel.BaseSearchViewModel.ListCountryID.Count() == 0 || ViewModel.BaseSearchViewModel.ListCountryID.Any(pl => pl == p.Address.City.Province.Country.CtryID)) &&
                                                    (ViewModel.BaseSearchViewModel.ListStateID.Count() == 0 || ViewModel.BaseSearchViewModel.ListStateID.Any(pl => pl == p.Address.City.Province.PrvID)) &&
                                                    (ViewModel.BaseSearchViewModel.Street == null || p.Address.Street.Contains(ViewModel.BaseSearchViewModel.Street)) &&
                                                    (ViewModel.BaseSearchViewModel.CivicNumber == null || p.Address.CivicNumber.Contains(ViewModel.BaseSearchViewModel.CivicNumber)) &&
                                                    (ViewModel.BaseSearchViewModel.ListGrpDescID1.Count() == 0 || p.GroupLocations.Select(sg => sg.GrpDescID).Intersect(ViewModel.BaseSearchViewModel.ListGrpDescID1).Any()) &&
                                                    (ViewModel.BaseSearchViewModel.ListGrpDescID2.Count() == 0 || p.GroupLocations.Select(sg => sg.GrpDescID).Intersect(ViewModel.BaseSearchViewModel.ListGrpDescID2).Any()) &&
                                                    (ViewModel.BaseSearchViewModel.ListGrpDescID3.Count() == 0 || p.GroupLocations.Select(sg => sg.GrpDescID).Intersect(ViewModel.BaseSearchViewModel.ListGrpDescID3).Any()) &&
                                                    (ViewModel.BaseSearchViewModel.ListGrpDescID4.Count() == 0 || p.GroupLocations.Select(sg => sg.GrpDescID).Intersect(ViewModel.BaseSearchViewModel.ListGrpDescID4).Any())


                                                        )
                               select new LocationViewModel()
                               {
                                   LocationID = s.LocationID,
                                   LocationTypeID = s.LocationTypeID,
                                   Long = s.Address.Longitude,
                                   Lat = s.Address.Latitude,
                                   FileNumber = s.LocationFile,
                                   State = s.Address.City.Province.PrvName,
                                   City = s.Address.City.CityName,
                                   Address = s.Address.CivicNumber + " " + s.Address.Street,
                                   Status = s.LocationType.LocationTypeTexts.FirstOrDefault(p => p.LangID == lang).Txt,
                                   DefaultImgPath = s.LocationPictures.FirstOrDefault(p => p.IsDefault == true && p.IsActive == true).FilePath,
                                   Location = s,
                                   HasPremises = s.Premises.Any(p => p.IsActive == true && p.IsDelete == false),
                                   ListGrpDescID = s.GroupLocations.Select(g => g.GrpDescID)
                               };

【问题讨论】:

    标签: asp.net-mvc-2 entity-framework-4


    【解决方案1】:

    你想要的是预编译 linq。这里有一篇关于这个主题的不错的 MSDN 文章: http://msdn.microsoft.com/en-us/magazine/ee336024.aspx

    我发现这个过程有点麻烦,并且发现我自己要恢复到存储过程来处理我需要编写的少数查询,这些查询会增加你的复杂程度。

    【讨论】:

      【解决方案2】:

      Microsoft 提供的关于预编译视图的指南可能是您最好的选择:

      How to: Pre-Generate Views to Improve Query Performance

      【讨论】:

      • 我已经为我的 .edmx 文件这样做了。这里的问题是,没有什么可以解释如何预先生成存储在存储库文件中的自定义查询。
      猜你喜欢
      • 2016-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多