开发者

Differences between query using LINQ and IQueryable interface directly?

开发者 https://www.devze.com 2023-02-02 03:32 出处:网络
Using Entity Framework 4, and given: ObjectSet<Thing> AllThings = Context.CreateObjectSet<Thing>;

Using Entity Framework 4, and given:

ObjectSet<Thing> AllThings = Context.CreateObjectSet<Thing>;

public IQuery开发者_开发技巧able<Thing> ByNameA(String name)
{
    IQueryable<Thing> query =  from o in AllThings 
                               where o.Name == name 
                               select o;
    return query;
}

public IQueryable<Thing> ByNameB(String name)
{
    return AllThings.Where((o) => o.Name == name);
}

Both return IQueryable<> instances, and thus the query doesn't hit the server until something like ToList() is called, right? Is it purely readability that is the difference, or are the using fundamentally different technologies on the back-end?


These compile to nearly identical code.

The first syntax translates directly, by the compiler, into methods with the names provided in the second syntax.

The main difference between these two methods is really just that you're using a different syntax, and that you're assigning to a temporary variable (query) instead of just returning the result directly. However, they are, for all practical purposes, identical.

0

精彩评论

暂无评论...
验证码 换一张
取 消