2

I have an ASP.NET MVC application. Let's suppose that I have this view:

@model IEnumerable<MyClass>
   .....

On the server side, I have a linq query, to my database, using EF:

public ActionResult Index()
{
    var query = from t in context.MyClass
                select t;

    //now comes the question
    return View(query);
    return View(query.AsEnumerable()); //is there any difference?
}

I think that the AsEnumerable() is not necessary because the query will automatically cast to it, so, can someone explain me when the AsEnumerable() is useful?

Thank you!

Fabio
  • 11,892
  • 1
  • 25
  • 41
  • What you think is basically right - the query already returns an object that implements `IEnumerable`. However see these threads: [one](http://stackoverflow.com/questions/2013846/why-use-asenumerable-rather-than-casting-to-ienumerablet), [two](http://stackoverflow.com/questions/17968469/whats-the-differences-between-tolist-asenumerable-asqueryable) for details about `AsEnumerable` (and `AsQueryable` btw) – Andrei May 27 '14 at 12:40

1 Answers1

1

It is not necessary. The query you have declared results in a sequence, which implements the IEnumerable interface.

As you will see here, the Select extension method of types that implement the IEnumerable, returns a IEnumerable.

public static IEnumerable<TResult> Select<TSource, TResult>(
this IEnumerable<TSource> source,
Func<TSource, TResult> selector)

Your query

var query = from t in context.MyClass
            select t;

will be compiled to

var query = context.MyClass.Select(x=>x);

hence I am refering to the Select extension method.

Regarding now the use of AsEnumerable()

The AsEnumerable(IEnumerable) method has no effect other than to change the compile-time type of source from a type that implements IEnumerable to IEnumerable itself.

Also

AsEnumerable(IEnumerable) can be used to choose between query implementations when a sequence implements IEnumerable but also has a different set of public query methods available. For example, given a generic class Table that implements IEnumerable and has its own methods such as Where, Select, and SelectMany, a call to Where would invoke the public Where method of Table. A Table type that represents a database table could have a Where method that takes the predicate argument as an expression tree and converts the tree to SQL for remote execution. If remote execution is not desired, for example because the predicate invokes a local method, the AsEnumerable method can be used to hide the custom methods and instead make the standard query operators available.

For further documentation, please have a look here.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • It seems like the last paragraph you pasted contradicts what you say and the OP's assumption. Unless you know that `View()` isn't going to call `Select` or `Where`, etc., different query implementations might be run, and this could be significant. – jwg May 27 '14 at 12:57
  • The way you have placed your argument makes sense. However, the OP uses entity framework to access the database. For that reason the entity classes that are created by entity framework if uses the database first approach or the entity classes he have built, if he uses the code first approach, don't have methods like Where, Select and so on. – Christos May 27 '14 at 13:03