The universal quantifier is written as an upside down

Info icon This preview shows pages 316–318. Sign up to view the full content.

View Full Document Right Arrow Icon
The universal quantifier is written as an upside down A ( ), and is con- ventionally pronounced “for all.” It corresponds to the All operator, and is true if all the elements in some set meet the condition. The con- vention that the universal quantifier is true for any empty set (i.e., that All returns true when you give it no elements, regardless of the condi- tion) has a splendid mathematical name: it is called a vacuous truth . Quantifiers are special cases of a more general operation called aggregation aggregation operators perform calculations across all the elements in a set. The quan- tifiers are singled out as special cases because they have the useful property that the calculation can often terminate early: if you’re testing to see whether something is true about all the elements in the set, and you find an element for which it’s not true, you can stop right there. But for most whole-set operations that’s not true, so there are some more general-purpose aggregation operators. Aggregation Aggregation operators perform calculations that involve every single element in a col- lection, producing a single value as the result. This can be as simple as counting the number of elements—this involves all the elements in the sense that you need to know how many elements exist to get the correct count. And if you’re dealing with an IEnumerable<T> , it is usually necessary to iterate through the whole collection because in general, enumerable sources don’t know how many items they contain in advance. So the Count operator iterates through the entire collection, and returns the number of elements it found. 292 | Chapter 8: LINQ
Image of page 316

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
LINQ to Objects has optimizations for some special cases. It looks for an implementation of a standard ICollection<T> interface, which de- fines a Count property. (This is distinct from the Count operator, which, like all LINQ operators, is a method, not a property.) Collections such as arrays and List<T> that know how many items they contain imple- ment this interface. So the Count operator may be able to avoid having to enumerate the whole collection by using the Count property. And more generally, the nature of the Count operator depends on the source—database LINQ providers can arrange for the database to cal- culate the correct value for Count , avoiding the need to churn through an entire table just to count rows. But in cases where there’s no way of knowing the count up front, such as the file enumeration in Exam- ple 8-1 , Count can take a long time to complete. LINQ defines some specialized aggregation operators for numeric values. The Sum op- erator returns the sum of the values of a given expression for all items in a collection. For example, if you want to find out how many hours of meetings you have in a col- lection of events, you could do this: double totalHours = events.Sum(ev => ev.Duration.TotalHours); Average calculates the same sum, but then divides the result by the number of items, returning the mean value.
Image of page 317
Image of page 318
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern