1

I am .net developer.I am good at the .net and also EF.I write EF code but there is performance problem(here is my question:Maximizing Performance with the Entity Framework)

I have performance problem and I could not fix that problem.So I decided to convert EF code first code to stored procedure or MSSQL

Here is my EF Code

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);
}

I converted that code to this.But it does not work right

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+'%'
Community
  • 1
  • 1
user1688401
  • 1,851
  • 8
  • 47
  • 83
  • Could you expand on "It does not work right"? Is an error raised, or is the output incorrect? – Steven Dec 21 '15 at 15:02
  • no error...output is incorrect..sp output is different from ef output so it is incorrect – user1688401 Dec 21 '15 at 15:14
  • I would profile to find out what SQL is generated by EF, by either [logging against the database connection](https://msdn.microsoft.com/en-us/data/dn469464.aspx#Log.Example) or running SQL Profiler - you can then run that query separately and compare to the output of running the sproc manually. You can then modify the sproc code until the output matches EF generated SQL if that is your goal. – user978139 Dec 21 '15 at 15:30

0 Answers0