© Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelLINQ via C# 3.0Chapter 4 – LINQ to Objects © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelLINQ 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 IsraelLINQ to Objects•LINQ to Objects relies on the Enumerable class, which contains query operators as extension methods•Any IEnumerable<T> can be queried•Additional types can be queried if they implement the query pattern © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelQuery 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 IsraelQuery Pattern: Selectclass PayrollSystem { public IEnumerable<T> Select<T>( Func<Employee,T> selector) { return employees.Select(selector); } internal List<Employee> employees = .; //More implementation omitted}PayrollSystem payroll = .;var names = from employee in payroll select employee.Name; © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelQuery Pattern: Select Extension•Alternatively, an extension method can be used:class PayrollSystem { internal List<Employee> employees = .; //More implementation omitted}static class PayrollExtensions { public static IEnumerable<T> Select<T>( this PayrollSystem payroll, Func<Employee,T> selector) { return payroll.employees.Select(selector); }}PayrollSystem payroll = .;var names = from employee in payroll select employee.Name; © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelQuery Pattern: Whereclass PayrollSystem { public IEnumerable<Employee> Where( Func<Employee,bool> filter) { return employees.Where(filter); } internal List<Employee> employees = .; //Select<T> omitted}PayrollSystem payroll = .;var names = from employee in payroll where employee.Salary > 220 select employee.Name; © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelFeature 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 IsraelLINQ to Objects Applicability•Any object model is a potential data source for LINQ queries–The requirements are not strict – either IEnumerable or the query pattern•Make your object models LINQ-friendly! © Copyright SELA Software & Education Labs Ltd. 14-18 Baruch Hirsch St. Bnei Brak 51202 IsraelLINQ to Stringsstring 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-23635452f6f9883706469852363545269 [...]... loop into a LINQ query 2 yield return is often slower than handrolled enumerators or for loops 3 Invoking lambdas is often more expensive than an inline loop body 4 Measure to ensure sane results and sane number of calls (run-time complexity) © Copyright SELA Software & Education Labs Ltd 1 4- 18 Baruch Hirsch St Bnei Brak 51202 Israel Chapter Summary • Using language integrated query operators with objects. .. Israel LINQ to WCF Contracts var operations = from operation in ContractDescription.GetContract( typeof(IService)).Operations where !operation.IsOneWay where operation.Faults.Any( f => f.DetailType == typeof(string)) select new { operation.DeclaringContract.Namespace, operation.SyncMethod.Name }; © Copyright SELA Software & Education Labs Ltd 1 4- 18 Baruch Hirsch St Bnei Brak 51202 Israel LINQ to Objects. .. Education Labs Ltd 1 4- 18 Baruch Hirsch St Bnei Brak 51202 Israel LINQ to File System //Find all large DLLs in System32 var largeDllFiles = from file in Directory.GetFiles( Environment.GetFolderPath( Environment.SpecialFolder.System)) let info = new FileInfo(file) where info.Extension == ".dll" && info.Length > 5000000 select info.FullName; © Copyright SELA Software & Education Labs Ltd 1 4- 18 Baruch Hirsch... 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 1 4- 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 . digitsOnly);//Output:8faf837d-f0a 6-4 e6 9-8 ac 5-2 363 545 2f6f988370 646 9852363 545 269 © Copyright SELA Software & Education Labs Ltd. 1 4- 18 Baruch Hirsch St. Bnei Brak 51202 IsraelLINQ. Ltd. 1 4- 18 Baruch Hirsch St. Bnei Brak 51202 IsraelLINQ via C# 3. 0Chapter 4 – LINQ to Objects © Copyright SELA Software & Education Labs Ltd. 1 4- 18