Linq To Objects
Linq To Objects
0
Chapter 4 LINQ to Objects
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
LINQ to Objects
Using language integrated query operators with objects Customizing query operators for particular objects Examples, examples, examples
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
LINQ to Objects
LINQ to Objects relies on the Enumerable class, which contains query operators as extension methods
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Query Pattern
If the type is not IEnumerable, there are two ways to make it queryable:
Add instance methods for query operators: Select<T>, Where, OrderBy, etc. Add extension methods in a separate class for query operators: Select<T>, Where, OrderBy, etc.
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
51202 Israel
Feature Notes
The query pattern implementation can also be used to customize query behavior
For example, specify additional filters for the Where query operator (even if the object is IEnumerable) static class PayrollExtensions { public static IEnumerable<Employee> Where( this PayrollSystem payroll, Func<Employee,bool> filter) { return payroll.employees.Where( e => e.City==London && filter(e)); } }
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
LINQ to Strings
string s = Guid.NewGuid().ToString(); string digitsOnly = new string((from c in s where Char.IsDigit(c) select c) .ToArray()); Console.WriteLine(s + \n + digitsOnly); //Output: 8faf837d-f0a6-4e69-8ac5-23635452f6f9 883706469852363545269
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
LINQ to Reflection
//Find all collection-initializable types //(Rough sketch) var queryableTypes = from asm in AppDomain.CurrentDomain.GetAssemblies() from t in asm.GetExportedTypes() where t.GetInterfaces().Any(itf => itf.IsGenericType && ((itf.IsGenericTypeDefinition && itf == typeof(ICollection<>)) || (!itf.IsGenericTypeDefinition && itf.GetGenericTypeDefinition() == typeof(ICollection<>))) ) || typeof(IEnumerable).IsAssignableFrom(t) && t.GetMethods().Any(m => m.Name == "Add") select t;
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel
Chapter Summary
Using language integrated query operators with objects Implementing the query pattern for nonIEnumerable objects Customizing the query behavior for specific objects
Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 Israel