The sake of itthey are more consistent with these

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

the sake of it—they are more consistent with these concepts’ names in database query languages. Most professional developers are currently likely to have rather more experience with SQL than, say, Haskell or LISP. Set Operations LINQ provides operators for some common set-based operations. If you have two col- lections, and you want to discover all the elements that are present in both collections, you can use the Intersect operator: var inBoth = set1.Intersect(set2); It also offers a Union operator, which provides all the elements from both input sets, but when it comes to the second set it will skip any elements that were already returned because they were also in the first set. So you could think of this as being like Concat , 294 | Chapter 8: LINQ
Image of page 318

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

except it detects and removes duplicates. In a similar vein, there’s the Distinct operator—this works on a single collection, rather than a pair of collections. Distinct ensures that it returns any given element only once, so if your input collection happens to contain duplicate entries, Distinct will skip over those. Finally, the Except operator returns only those elements from the first set that do not also appear in the second set. Joining LINQ supports joining of sources, in the sense typically associated with databases— given two sets of items, you can form a new set by combining the items from each set that have the same value for some attribute. This is a feature that tends not to get a lot of use when working with object models—relationships between objects are usually represented with references exposed via properties, so there’s not much need for joins. But joins can become much more important if you’re using LINQ with data from a relational database. (Although the Entity Framework, which we describe in a later chapter, is often able to represent relationships between tables as object references. It’ll use joins at the database level under the covers, but you may not need to use them explicitly in LINQ all that often.) Even though joins are typically most useful when working with data structured for storage in a relational database, you can still perform joins across objects—it’s possible with LINQ to Objects even if it’s not all that common. In our hypothetical calendar application, imagine that you want to add a feature where you can reconcile events in the user’s local calendar with events retrieved from his phone’s calendar, and you need to try to work out which of the imported events from the phone correspond to items already in the calendar. You might find that the only way to do this is to look for events with the same name that occur at the same time, in which case you might be able to use a join to build up a list of events from the two sources that are logically the same events: var pairs = from localEvent in events join phoneEvent in phoneEvents on new { Title = localEvent.Title, Start = localEvent.StartTime } equals new { Title = phoneEvent.Name, Start = phoneEvent.Time } select new { Local = localEvent, Phone = phoneEvent };
Image of page 319
Image of page 320
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15

{[ 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