13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Extension methods <strong>in</strong> .NET 3.5269Now that we’ve seen how all these apply to our “collection of numbers” example, it’stime for me to make good on the promise of some more bus<strong>in</strong>ess-related situations.10.3.5 Bus<strong>in</strong>ess examples <strong>in</strong>volv<strong>in</strong>g cha<strong>in</strong><strong>in</strong>gMuch of what we do as developers <strong>in</strong>volves mov<strong>in</strong>g data around. In fact, for manyapplications that’s the only mean<strong>in</strong>gful th<strong>in</strong>g we do—the user <strong>in</strong>terface, web services,database, and other components often exist solely to get data from one placeto another, or from one form <strong>in</strong>to another. It should be of no surprise that theextension methods we’ve looked at <strong>in</strong> this section are well suited to many bus<strong>in</strong>essproblems. I’ll just give a couple of examples, as I’m sure you’ll be able to take themas a spr<strong>in</strong>gboard <strong>in</strong>to th<strong>in</strong>k<strong>in</strong>g about your bus<strong>in</strong>ess requirements and how <strong>C#</strong> 3 andthe Enumerable class can help you solve problems more expressively than before.For each example I’ll only <strong>in</strong>clude a sample query—it should be enough to understandthe purpose of the code, but without all the baggage. Full work<strong>in</strong>g code is onthe book’s website.AGGREGATION: SUMMING SALARIESThe first example <strong>in</strong>volves a company comprised of several departments. Each departmenthas a number of employees, each of whom has a salary. Suppose we want toreport on total salary cost by department, with the most expensive department first.The query is simplycompany.Departments.Select(dept => new{dept.Name,Cost=dept.Employees.Sum (person => person.Salary)}).OrderByDescend<strong>in</strong>g (deptWithCost => deptWithCost.Cost);This query uses an anonymous type to keep the department name (us<strong>in</strong>g a projection<strong>in</strong>itializer) and the sum of the salaries of all the employees with<strong>in</strong> that department.The salary summation uses a self-explanatory Sum extension method, aga<strong>in</strong> part ofEnumerable. In the result, the department name and total salary can be retrieved asproperties. If you wanted the orig<strong>in</strong>al department reference, you’d just need tochange the anonymous type used <strong>in</strong> the Select method.GROUPING: COUNTING BUGS ASSIGNED TO DEVELOPERSIf you’re a professional developer, I’m sure you’ve seen many project managementtools giv<strong>in</strong>g you different metrics. If you have access to the raw data, LINQ can helpyou transform it <strong>in</strong> practically any way you choose. As a simple example, we could lookat a list of developers and how many bugs they have assigned to them at the moment:bugs.GroupBy(bug => bug.AssignedTo).Select(list => new { Developer=list.Key, Count=list.Count() }).OrderByDescend<strong>in</strong>g (x => x.Count);This query uses the GroupBy extension method, which groups the orig<strong>in</strong>al collectionby a projection (the developer assigned to fix the bug <strong>in</strong> this case), result<strong>in</strong>g <strong>in</strong> anLicensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!