如何提高EF Linq对实体的性能 [英] How to boost performance EF Linq to Entities

查看:55
本文介绍了如何提高EF Linq对实体的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用asp.net 4 c#和EF 4.

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.";

谢谢

有用的文章 http://weblogs.asp.net/zeeshanhirani/archive/2010/09/20/which-one-is-faster-singleordefault-or-firstordefault.aspx

推荐答案

使用FirstOrDefault() ...找到结果后将立即退出.

Use FirstOrDefault() ... This will exit as soon as it finds a result.

另一方面,SingleOrDefault()在整个集合中搜索单个结果,如果它为查询找到多个结果,则抛出异常

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

这篇关于如何提高EF Linq对实体的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆