Linq对实体与ESQL的性能 [英] Performance of Linq to Entities vs ESQL

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

问题描述

使用实体框架时,ESQL的表现要比Linq对实体好吗?



我更喜欢使用Linq to Entities(主要是因为强式检查),但是我的其他一些团队成员则引用性能作为使用ESQL的一个原因。我想得到一个使用任一方法的pro / con的完整想法。

解决方案

最明显的区别是: / p>

Linq to Entities是强类型代码,包括很好的查询理解语法。 选择之前的来自允许IntelliSense帮助您。



实体SQL使用传统的基于字符串的查询,具有更熟悉的SQL语法,其中SELECT语句来自FROM。因为eSQL是基于字符串的,因此动态查询可能会在运行时使用字符串操作以传统的方式组合。



不太明显的主要区别是:



Linq to Entities允许您使用select new {...}语法更改形状或将查询结果项目化为任何需要的形状。匿名类型,新到C#3.0,已经允许这个。



使用Entity SQL不可能投影,因为您必须始终返回ObjectQuery< T>。在某些情况下,可以使用ObjectQuery< object>但是,您必须解决这个事实.Select总是返回ObjectQuery< DbDataRecord> ;.请参阅下面的代码...

  ObjectQuery< DbDataRecord> query = DynamicQuery(context,
Products,
it.ProductName ='Chai',
it.ProductName,it.QuantityPerUnit);

public static ObjectQuery< DbDataRecord> DynamicQuery(MyContext context,string root,string selection,string projection)
{
ObjectQuery< object> rootQuery = context.CreateQuery< object>(root);
ObjectQuery< object> filteredQuery = rootQuery.Where(选择);
ObjectQuery< DbDataRecord> result = filteredQuery.Select(projection);
返回结果;
}

其中一个团队成员详细描述了其他更微妙的差异< a href =http://blogs.msdn.com/diego/archive/2007/12/20/some-differences-between-esql-and-linq-to-entities-capabilities.aspx =noreferrer>这里和这里


When using the Entity Framework, does ESQL perform better than Linq to Entities?

I'd prefer to use Linq to Entities (mainly because of the strong-type checking), but some of my other team members are citing performance as a reason to use ESQL. I would like to get a full idea of the pro's/con's of using either method.

解决方案

The most obvious differences are:

Linq to Entities is strongly typed code including nice query comprehension syntax. The fact that the "from" comes before the "select" allows IntelliSense to help you.

Entity SQL uses traditional string based queries with a more familiar SQL like syntax where the SELECT statement comes before the FROM. Because eSQL is string based, dynamic queries may be composed in a traditional way at run time using string manipulation.

The less obvious key difference is:

Linq to Entities allows you to change the shape or "project" the results of your query into any shape you require with the "select new{... }" syntax. Anonymous types, new to C# 3.0, has allowed this.

Projection is not possible using Entity SQL as you must always return an ObjectQuery<T>. In some scenarios it is possible use ObjectQuery<object> however you must work around the fact that .Select always returns ObjectQuery<DbDataRecord>. See code below...

ObjectQuery<DbDataRecord> query = DynamicQuery(context,
        "Products",
        "it.ProductName = 'Chai'",
        "it.ProductName, it.QuantityPerUnit");

public static ObjectQuery<DbDataRecord> DynamicQuery(MyContext context, string root, string selection, string projection)
{
    ObjectQuery<object> rootQuery = context.CreateQuery<object>(root);
    ObjectQuery<object> filteredQuery = rootQuery.Where(selection);
    ObjectQuery<DbDataRecord> result = filteredQuery.Select(projection);
    return result;
}

There are other more subtle differences described by one of the team members in detail here and here.

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

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