I use asp.net 4 c# and EF 4.
I’m profiling my application. I have this code which result expensive.
I woud like to know if you know a better way to write it. I need to speed it up.
string htmlhead = context.CmsOptions.SingleOrDefault(op => op.OptionId == 7).Value;
if (htmlhead != null)
uxHtmlHead.Text = htmlhead;
else
uxHtmlHead.Text = "No Html Head.";
Thanks
Useful article
http://weblogs.asp.net/zeeshanhirani/archive/2010/09/20/which-one-is-faster-singleordefault-or-firstordefault.aspx
Use
FirstOrDefault()… This will exit as soon as it finds a result.On the other hand
SingleOrDefault()searches the entire collection for a single result and throws exception if it finds more than one result for the query