I have this linq query:
public static Banner getSideBarBanner(){
DataClassesDataContext db = new DataClassesDataContext();
var bannerSiderBar = (from b in db.Banners
where b.Position.Equals(EBannersPosition.siderbar.ToString())
&& b.Visible == true
select b).FirstOrDefault();
return bannerSiderBar;
}
well, I use dotTrace to profile the application and I see that the query execution takes a lot of time (over 2s)
I am simply wondering, why so much time especially when my Banner table has about 30 records!!!
Thanks in advance for your opionions...
UPDATE: Banner's table schema:
UPDATE 2: If I use simple SQL connection instead of linq, the query execution takes 700ms which is a huge improvement...
public static Banner getSideBarBanner()
{
Banner bannerFound = new Banner();
SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Library_prodConnectionString"].ConnectionString);
try
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand("SELECT path, link FROM Banner b WHERE b.Position = @position AND b.Visible = 1 ", myConnection);
myCommand.Parameters.Add(new SqlParameter("@position", EBannersPosition.siderbar.ToString()));
SqlDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
if (myReader["path"] != null)
bannerFound.Path = myReader["path"].ToString();
if (myReader["link"] != null)
bannerFound.Link = myReader["link"].ToString();
}
myConnection.Close();
}
catch (Exception e)
{
CreateLogFiles Err = new CreateLogFiles();
Err.ErrorLog(HttpContext.Current.Server.MapPath("~/Site/Logs/ErrorLog"), e.ToString());
}
return bannerFound;
}
This tells me that translation of the linq query to sql has a very poor performance...What do you think?