C# - AsEnumerable Example

前端 未结 9 1328
耶瑟儿~
耶瑟儿~ 2020-12-20 00:44

What is the exact use of AsEnumerable? Will it change non-enumerable collection to enumerable collection?.Please give me a simple example.

相关标签:
9条回答
  • 2020-12-20 01:12

    From the "Remarks" section of the MSDN documentation:

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

    AsEnumerable<TSource> can be used to choose between query implementations when a sequence implements IEnumerable<T> but also has a different set of public query methods available. For example, given a generic class Table that implements IEnumerable<T> 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<TSource> method can be used to hide the custom methods and instead make the standard query operators available.

    0 讨论(0)
  • 2020-12-20 01:12
    static void Main()
        {
            /* 
            "AsEnumerable" purpose is to cast an IQueryable<T> sequence to IEnumerable<T>, 
            forcing the remainder of the query to execute locally instead of on database as below example so it can hurt performance.  (bind  Enumerable operators instead of Queryable). 
    
            In below example we have cars table in SQL Server and are going to filter red cars and filter equipment with some regex:
            */
            Regex wordCounter = new Regex(@"\w");
            var query = dataContext.Cars.Where(car=> article.Color == "red" && wordCounter.Matches(car.Equipment).Count < 10);
    
            /* 
            SQL Server doesn’t support regular expressions  therefore the LINQ-to-db  providers  will  throw  an  exception: query  cannot  be translated to SQL.
    
            TO solve this firstly we can get all cars with red color using a LINQ to SQL query, 
            and secondly filtering locally for Equipment of less than 10 words:
            */
    
            Regex wordCounter = new Regex(@"\w");
    
            IEnumerable<Car> sqlQuery = dataContext.Cars
              .Where(car => car.Color == "red");
            IEnumerable<Car> localQuery = sqlQuery
              .Where(car => wordCounter.Matches(car.Equipment).Count < 10);
    
            /*
            Because sqlQuery is of type IEnumerable<Car>, the second query binds  to the local query operators,
            therefore that part of the filtering is run on the client.
    
            With AsEnumerable, we can do the same in a single query:
    
             */
            Regex wordCounter = new Regex(@"\w"); 
            var query = dataContext.Cars
              .Where(car => car.Color == "red")
              .AsEnumerable()
              .Where(car => wordCounter.Matches(car.Equipment).Count < 10);
    
            /*
            An alternative to calling AsEnumerable is ToArray or ToList.
            */
        }
    
    0 讨论(0)
  • 2020-12-20 01:15

    If you take a look in reflector:

    public static IEnumerable<TSource> AsEnumerable<TSource>(this IEnumerable<TSource> source)
    {
        return source;
    }
    

    It basically does nothing more than down casting something that implements IEnumerable.

    0 讨论(0)
提交回复
热议问题