我是.net开发者。我擅长.net和EF.I写EF代码但是存在性能问题(这是我的问题:Maximizing Performance with the Entity Framework)
我遇到了性能问题,我无法解决这个问题。所以我决定将EF代码的第一个代码转换为存储过程或MSSQL
这是我的EF代码
List<Tuple<string, int, int>> result = new List<Tuple<string, int, int>>();
using (var db = new TourismContext())
{
ModelState.Remove(q);
var query = SearchWordFunctions.WordFunctions(q);
var ListCity = db.CityTranslations.Where(
c => (c.Slug.StartsWith(query) || c.Name.StartsWith(query))
&&
c.City.Latitude.HasValue
).GroupBy(x => x.CityID).Select(g => g.FirstOrDefault()).Take(10);
var ListRegion = db.RegionTranslations.Where(
r => (r.Slug.StartsWith(query) || r.Name.StartsWith(query))
&&
r.Region.Latitude.HasValue
&&
r.Region.RefID == 0 && r.Region.IsShow > 0
).GroupBy(x => x.RegionID).Select(g => g.FirstOrDefault()).Take(10);
var LandMark = db.CityLandMarks.Where(l => l.Translations.Any(t => t.Name.StartsWith(query)) && l.Latitude.HasValue).Take(10);
var hotel = db.HotelTranslations.Where(t => t.Url.Contains(query) && t.Hotel.Status > 0 && t.Culture.Code == culture).ToList();
result.Clear();
foreach (var item in ListCity.OrderBy(o => o.Name.Length))
{
result.Add(new Tuple<string, int, int>(string.Concat(item.Name, " - <b>", item.City.Country.Translations.Single(t => t.CultureID == 1).Name, "<b>"), item.CityID, 1));
if (db.Regions.Any(r => r.CityID == item.CityID))
{
var regions = db.Regions.Where(r => r.CityID == item.CityID && r.Latitude.HasValue && r.RefID == 0 && r.IsShow > 0).GroupBy(g => g.ID).Select(x => x.FirstOrDefault()).ToList().OrderByDescending(o => o.SearchRating).Take(10);
foreach (var regItem in regions)
{
result.Add(new Tuple<string, int, int>(string.Concat(regItem.Translations.FirstOrDefault().Name, " - <b>", item.Name, "</b> - <b>", regItem.City.Country.Translations.FirstOrDefault().Name, "<b>"), regItem.ID, 2));
}
}
}
if (ListCity.Count() <= 0)
{
foreach (var item in ListRegion)
{
result.Add(new Tuple<string, int, int>(string.Concat(item.Name, " - <b>", item.Region.City.Translations.Single(t => t.Culture.Code == culture).Name, "</b> - <b>", item.Region.City.Country.Translations.Single(t => t.Culture.Code == culture).Name, "</b>"), item.RegionID, 2));
}
}
foreach (var item in LandMark)
{
result.Add(new Tuple<string, int, int>(string.Concat(item.Translations.FirstOrDefault().Name, " - <b>", item.City.Translations.FirstOrDefault().Name, "</b> - <b>", item.City.Country.Translations.FirstOrDefault().Name, "</b>"), item.ID, 3));
}
foreach (var item in hotel)
{
result.Add(new Tuple<string, int, int>(string.Concat(item.Name, " - <b class=\"refid\" data=\"" + item.HotelID + "\">", item.Hotel.Region.City.Translations.First().Name, "</b>"), item.Hotel.Region.CityID, 1));
}
}
return Json(result, JsonRequestBehavior.AllowGet);
}
我将该代码转换为此。但它无法正常工作
GO
ALTER PROCEDURE [dbo].[GetHotelRegions]
(
@query nvarchar(30),
@culture bit
)
AS
select distinct top 400 ct.Name,co.Code,rt.Name,r.ID as RID,ht.Name,c.ID,h.ID
--(ct.Name +' - <b>'+co.Code+'<b>') as CityName,
--(rt.Name +' - <b>'+ct.Name+'</b> - <b>'+co.Code+'<b>') as RegionName,r.ID as RID,
--ht.Name as HotelName,
--(ht.Name +' - <b class=\"refid\" data=\"'+CONVERT(varchar(2),ht.ID)+'\'+ct.Name+'</b>') as HotelName,
--c.ID
from CityTranslations ct (nolock)
inner join Cities c (nolock) on c.ID=ct.CityID and c.Latitude is not null
inner join Regions r (nolock) on r.CityID = ct.CityID and r.RefID<=0
inner join RegionTranslations rt (nolock) on r.ID=rt.RegionID and rt.CultureID=@culture
inner join Hotels h (nolock) on h.RegionID=r.ID and h.Status>0
inner join HotelTranslations ht (nolock) on h.ID=ht.HotelID and rt.CultureID=@culture
inner join Countries co (nolock) on co.ID=c.CountryID
where ct.Slug LIKE @query+'%'
or rt.Slug LIKE @query +'%'
or ht.Url LIKE @query+'%'