Why are ToLookup and GroupBy different?

后端 未结 3 1481
情书的邮戳
情书的邮戳 2020-11-29 22:10

.ToLookup returns an ILookup. ILookup also implements interface I

3条回答
  •  鱼传尺愫
    2020-11-29 22:37

    why would I ever bother with GroupBy? Why should it exist?

    What happens when you call ToLookup on an object representing a remote database table with a billion rows in it?

    The billion rows are sent over the wire, and you build the lookup table locally.

    What happens when you call GroupBy on such an object?

    A query object is built; end of story.

    When that query object is enumerated then the analysis of the table is done on the database server and the grouped results are sent back on demand a few at a time.

    Logically they are the same thing but the performance implications of each are completely different. Calling ToLookup means I want a cache of the entire thing right now organized by group. Calling GroupBy means "I am building an object to represent the question 'what would these things look like if I organized them by group?'"

提交回复
热议问题