C# - AsEnumerable Example

前端 未结 9 1327
耶瑟儿~
耶瑟儿~ 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 00:48

    AsEnumerable can only be used on enumerable collections. It just changes the type of the collection to IEnumerable<T> to access more easily the IEnumerable extensions.

    0 讨论(0)
  • 2020-12-20 00:56

    Here's example code which may illustrate LukeH's correct explanation.

    IEnumerable<Order> orderQuery = dataContext.Orders
      .Where(o => o.Customer.Name == "Bob")
      .AsEnumerable()
      .Where(o => MyFancyFilterMethod(o, MyFancyObject));
    

    The first Where is Queryable.Where, which is translated into sql and run in the database (o.Customer is not loaded into memory).

    The second Where is Enumerable.Where, which calls an in-memory method with an instance of something I don't want to send into the database.

    Without the AsEnumerable method, I'd have to write it like this:

    IEnumerable<Order> orderQuery =
      ((IEnumerable<Order>)
        (dataContext.Orders.Where(o => o.Customer.Name == "Bob")))
      .Where(o => MyFancyFilterMethod(o, MyFancyObject));
    

    Or

    IEnumerable<Order> orderQuery =
      Enumerable.Where(
        dataContext.Orders.Where(o => o.Customer.Name == "Bob"),
        (o => MyFancyFilterMethod(o, MyFancyObject));
    

    Neither of which flow well at all.

    0 讨论(0)
  • 2020-12-20 00:58

    Nobody has mentioned this for some reason, but observe that something.AsEnumerable() is equivalent to (IEnumerable<TSomething>) something. The difference is that the cast requires the type of the elements to be specified explicitly, which is, of course, inconvenient. For me, that's the main reason to use AsEnumerable() instead of the cast.

    0 讨论(0)
  • 2020-12-20 00:59

    AsEnumerable() converts an array (or list, or collection) into an IEnumerable<T> of the collection.

    See http://msdn.microsoft.com/en-us/library/bb335435.aspx for more information.

    From the above article:

    The AsEnumerable<TSource>(IEnumerable<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.
    0 讨论(0)
  • 2020-12-20 01:06

    After reading the answers, i guess you are still missing a practical example.

    I use this to enable me to use linq on a datatable

    var mySelect = from table in myDataSet.Tables[0].AsEnumerable()
                where table["myColumn"].ToString() == "Some text"
                select table;
    
    0 讨论(0)
  • 2020-12-20 01:06

    No it doesn't change a non-enumerable collection to an enumerable one. What is does it return the collection back to you as an IEnumerable so that you can use it as an enumerable. That way you can use the object in conjunction with IEnumerable extensions and be treated as such.

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