Enterprise Framework LINQ Queries failing – MSDN Forums

Good Linq post from Colin Meek below:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2078990&SiteID=1 

To give a sense of what is shared and what isn’t between LINQ to SQL and LINQ to Entities (both in terms of modules and behavior) I’ll repeat some information that’s probably familiar to most people on this thread…

LINQ providers support integrated query via the IQueryable and IQueryProvider interfaces. The provider’s job is basically to translate LINQ expression trees into native queries. LINQ to SQL and LINQ to Entities accomplish this translation in roughly the same way, though nothing is shared beyond the System.Linq components in System.Core.dll.

Matt Warren’s IQueryable series gives a more detailed overview (http://blogs.msdn.com/mattwar/archive/2007/07/30/linq-building-an-iqueryable-provider-part-i.aspx) but I’ll include the highlights here to provide some context:

1. At compile time, user code is translated into (or directly uses) expression builder calls.

2. At runtime, calls to System.Linq.Queryable methods yield additional expression trees with argument expressions inline.

3. The provider is passed a LINQ expression which it translates into a “query” expression. LINQ to SQL and LINQ to Entities use different internal representations of the expression (a variant of LINQ expressions and Canonical Query Trees respectively). This step includes the replacement of method calls with query operators and the replacement of member accesses with store function calls.

4. Each provider then compensates for non-relational structures in the query (e.g. types, nested collections in results, etc.)

5. Native queries are generated and executed.

6. Query results are reshaped (basically the inverse of step 4).

As I mentioned, there is really nothing shared between LINQ to SQL and LINQ to Entities beyond step 2. The example in this thread illustrates a couple of differences between the two stacks in the handling of step 3 and step 4.

Implicit vs. explicit client evaluation

LINQ to SQL is a hybrid provider in the sense that it supports evaluation of parts of the query in the client and parts in the store. As a result, the following query succeeds in LINQ to SQL but fails in LINQ to Entities:

var query = from o in context.Orders

where GetCutoffDate() > o.OrderDate

select o;

As Brian suggested, it must be rewritten as:

DateTime cutoffDate = GetCutoffDate();

var query = from o in context.Orders

where cutoffDate > o.OrderDate

select o;

This makes the boundary between client and store evaluation explicit. There is a tradeoff between self-containment of queries and predictability of behavior for non-expert users.

Canonical functions vs. store functions

Of course, the sub-expression mentioned in this thread could be entirely translated into Transact-SQL function calls:

DateTime.UtcNow.AddDays(-180)

becomes

DATEADD(day, -180, GETUTCDATE())

Note that although LINQ to SQL could translate the above expression into Transact-SQL, it chooses to evaluate it on the client because it is uncorrelated. The behavior is different depending on whether results are correlated or not.

In Beta 2, LINQ to Entities does not support the DateTime.AddDays method simply because a corresponding “canonical function” does not exist. Canonical functions are store agnostic and can be implemented by arbitrary Entity Frameworks providers. We plan on introducing a few more DateTime-related functions in the next release (the usual disclaimers apply) at which point this expression will be supported by LINQ to Entities.

Regarding Rick’s question: Entity-SQL supports both canonical and store-specific functions. For functions not supported by LINQ to Entities, this provides the cleanest workaround. Where the gap is due to unsupported CLR methods – in either LINQ to SQL or LINQ to Entities – consider explicitly breaking up the query into client- and store- side elements. For instance, to extend the above example:

DateTime cutoffDate = GetCutoffDate();

var query = from o in context.Orders

where cutoffDate > o.OrderDate

select o;

var hybridQuery = from row in query.AsEnumerable()

select ShapeResult(row.Oid, row.Description, row.OrderDate);

Recommendation:

When working with LINQ to Entities, keep in mind that boundaries between client- and store- execution are explicit. To use a value computed on the client within a query, assign the value to a local variable before executing (see the assignment to cutoffDate above). To perform client-side processing of results, use the AsEnumerable method to yield results from the store query and then apply client-side logic (see the assignment to hybridQuery above).

Thanks,
-Colin

Enterprise Framework LINQ Queries failing – MSDN Forums

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

3 Responses to Enterprise Framework LINQ Queries failing – MSDN Forums

  1. Unknown says:

    http://www.batteryfast.com/asus/f3.htm Asus f3 Battery http://www.batteryfast.com/toshiba/pa3594u-1brs.htm Toshiba pa3594u-1brs Battery http://www.batteryfast.com/toshiba/pa3593u-1bas.htm Toshiba pa3593u-1bas Battery http://www.batteryfast.com/toshiba/pabas111.htm Toshiba pabas111 Battery http://www.batteryfast.com/dell/xps-m1530.htm Dell xps m1530 Battery http://www.batteryfast.com/hp/tx2000.htm Hp tx2000 Battery http://www.batteryfast.com/hp/pavilion-tx1000.htm Hp pavilion tx1000 Battery http://www.batteryfast.com/dell/xps-m1730.htm Dell xps m1730 Battery http://www.batteryfast.com/hp/pavilion-dv6.htm Hp pavilion dv6 Battery http://www.batteryfast.com/toshiba/pa3395u-1brs.htm Toshiba pa3395u-1brs Battery http://www.batteryfast.com/toshiba/pa3421u-1brs.htm Toshiba pa3421u-1brs Battery http://www.batteryfast.com/toshiba/pa3451u-1brs.htm Toshiba pa3451u-1brs Battery http://www.batteryfast.com/toshiba/pa3399u-1brs.htm Toshiba pa3399u-1brs Battery http://www.batteryfast.com/toshiba/pa3399u-2bas.htm Toshiba pa3399u-2bas Battery http://www.batteryfast.com/toshiba/satellite-a100.htm Toshiba satellite a100 Battery http://www.batteryfast.com/toshiba/satellite-m40.htm Toshiba satellite m40 Battery http://www.batteryfast.com/toshiba/satellite-m45.htm Toshiba satellite m45 Battery http://www.batteryfast.com/toshiba/satellite-m55.htm Toshiba satellite m55 Battery http://www.batteryfast.com/toshiba/pa3356u.htm Toshiba pa3356u Battery http://www.batteryfast.com/toshiba/pa3356u-1bas.htm Toshiba pa3356u-1bas Battery http://www.batteryfast.com/toshiba/pa3356u-1brs.htm Toshiba pa3356u-1brs Battery http://www.batteryfast.com/toshiba/pa3356u-2brs.htm Toshiba pa3356u-2brs Battery http://www.batteryfast.com/toshiba/pa3456u-1brs.htm Toshiba pa3456u-1brs Battery http://www.batteryfast.com/toshiba/portege-m500.htm Toshiba portege m500 Battery http://www.batteryfast.com/toshiba/pa3451u.htm Toshiba pa3451u Battery http://www.batteryfast.com/toshiba/pabas067.htm Toshiba pabas067 Battery http://www.batteryfast.com/toshiba/pa3465u-1brs.htm Toshiba pa3465u-1brs Battery http://www.batteryfast.com/toshiba/pa3534u-1brs.htm Toshiba pa3534u-1brs Battery http://www.batteryfast.com/toshiba/pa3533u-1bas.htm Toshiba pa3533u-1bas Battery http://www.batteryfast.com/toshiba/satellite-a205.htm Toshiba satellite a205 Battery http://www.batteryfast.com/toshiba/pa3534u-1bas.htm Toshiba pa3534u-1bas Battery http://www.batteryfast.com/uniwill/255-3s4400-g1l1.htm Uniwill 255-3s4400-g1l1 Battery http://www.batteryfast.com/uniwill/un255.htm Uniwill un255 Battery

  2. Unknown says:

    wow gold wow gold wow gold Silkroad gold wow power leveling wow power leveling wow power leveling warhammer gold warhammer gold warhammer gold buy warhammer gold buy warhammer gold buy warhammer gold warhammer gold warhammer gold warhammer gold buy warhammer gold buy warhammer gold buy warhammer gold Lotro gold Lotro gold Lotro gold Ffxi gil Ffxi gil Ffxi gil Maple story mesos Maple story mesos Maple story mesos buy wow gold buy wow gold buy wow gold cheap wow gold cheap wow gold cheap wow gold Lotr gold Lotr gold Lotr gold Aoc gold Aoc gold Aoc gold Age of conan gold Age of conan gold Age of conan gold Aoc power leveling Aoc power leveling Aoc power leveling cheap ffxi gil cheap ffxi gil cheap ffxi gil Final fantasy xi gil Final fantasy xi gil Final fantasy xi gil Flyff penya Flyff penya Flyff penya Maple story meso Maple story meso Maple story meso Maplestory mesos Maplestory mesos Maplestory mesos Runescape gold Runescape gold Runescape gold Runescape money Runescape money Runescape money Silkroad gold Silkroad gold Silkroad gold Lotro gold Maple story mesos Aoc gold warhammer gold Lotro gold Maple story mesos

  3. 小西 says:

    小跑过来看看~~~~

Comments are closed.