7

What is the Any() doing in the following query?

context.Customers
    .Include("InternetSales")
    .Where(c => c.InternetSales.Any())
    .Take(100);

How would you read out this query in plain English? For example, would the following be accurate?

"Get customers with their associated 100 internet sales."

(I know there is no "get" in the code, but you get what I mean.)

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
NoChance
  • 5,632
  • 4
  • 31
  • 45

1 Answers1

20

The Any operator checks whether some enumerable / collection contains at least one item, i.e. whether it is non-empty.

So I guess your query could read as:

"the first 100 customers that have made at least one internet sale"

or, somewhat closer to the metal:

"the first 100 Customer objects that have a non-empty InternetSales collection"

.Any() is similar to .Count() > 0, but it will consume at most one item in the collection, while Count consumes the complete collection, so Any is generally more efficient and works for infinite sequences, too. Provided you're not interested in the exact number of items, Any also expresses the intent of checking for non-emptiness more clearly.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • Perhaps a NotEmpty() synonym would be nice :) – NetMage Jun 19 '14 at 18:15
  • @NetMage: IMHO, it is far easier to intuitively understand the meaning of `customers.Any()` than of `customers.NotEmpty()`. With the latter, I will have to keep my brain from puzzling about the concept of non-empty customers: "Are they like milk bottles, i.e. they can be empty or full or something in-between?" OTOH, with `customers.Any()`, the intended meaning is immediately obvious: "Are there any customers?" – stakx - no longer contributing Jun 19 '14 at 20:16
  • @NetMage: Or, phrasing it differently, `customers.Any()` focuses on what `customers` represents (a group of customers), while `customers.NotEmpty()` focuses on what `customers` technically is (a collection object). The former point of view is usually more valuable to understanding what the code means. (Again, IMHO.) – stakx - no longer contributing Jun 19 '14 at 20:18
  • The fact that the first line of your answer explains .Any() in terms of non-empty makes me think otherwise :) – NetMage Jul 25 '14 at 18:00
  • @NetMage: You are right to a certain extent, and I guess I could improve my answer there. I added the above two comments quite some time after writing the original answer, and have probably learnt some things since then. – stakx - no longer contributing Jul 28 '14 at 06:40
  • @NetMage: I think it's important to distinguish between the explanation of a LINQ operator's *function* and the explanation for its *name*. Any *generally valid* explanation of a LINQ operator's *function* must necessarily rely on the same general abstraction that all things LINQ share: `IEnumerable` collections. Which is why my answer correctly gives a technical explanation. The *naming* of an operator OTOH is an entirely different matter: There's no reason that an operator's name cannot go beyong the technical POV; a name that leads to intuitive, easy-to-read code should be preferred. – stakx - no longer contributing Jul 28 '14 at 06:42
  • To beat a dead horse, since customers is "a group", whether a group is non-empty seems to me more understandable than "a group".Any which doesn't seem to have a sensible interpretation. We are not asking Customer.Any but rather Customers.Any. – NetMage Oct 22 '14 at 18:31