What is the difference between LINQ query expressions and extension methods

后端 未结 8 1088
一个人的身影
一个人的身影 2020-12-23 15:39

Below are two queries that return the same data. Other then style I am not sure which is better.

What factors influence these queries? What are the benefits of us

8条回答
  •  长情又很酷
    2020-12-23 15:55

    I think your question is better phrased like this, "What is the difference between IEnumerable and IQueryable with respect to LINQ"

    LINQ queries return an IQueryable by default. IQueryable allows you to append other filters or "clauses" onto your query before you execute it.

    Your LINQ query (first example) and your LINQ using method chaining (second example) produce the same result, with different syntax.

    It is possible to write a LINQ query as a LINQ method chain and visa versa. It really depends on your preference.

    @Lucas: The different is IEnumerable does in-memory querying and IQueryable does out-of-memory. Meaning, once you are in a foreach iterator, you are using IEnumerable, and when you are building your query, via either extension methods or using LINQ from o in object synatax, you are building an IQueryable. The IQueryable is executed as soon as you touch the Enumerator.

提交回复
热议问题