Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training...
-
Upload
khalid-brinton -
Category
Documents
-
view
216 -
download
0
Transcript of Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training...
![Page 1: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/1.jpg)
Deep Dive into LINQDeep Dive into LINQ
Eran Sharabi.NET Development Team LeaderJohnBryce [email protected]
![Page 2: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/2.jpg)
AgendaAgenda
• Building LINQ from C# 2.0• Deferred vs. Non-Deferred Execution• Enumerable vs. Queryable• Types of LINQ and Tools• Q & A• Summary
Tips and Best Practices
![Page 3: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/3.jpg)
Building LINQ from C# 2.0Building LINQ from C# 2.0
• Generics and Iterators in C# 2.0• Lambda expressions• Extension methods• System.LINQ• LINQ operators
![Page 4: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/4.jpg)
DEMODEMOBuilding LINQ from C# 2.0
![Page 5: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/5.jpg)
DEMODEMODeferred vs. Non-Deferred Execution
![Page 6: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/6.jpg)
var bad = from e in GetEmployees() from p in GetProjects() where e.GetDepartment() == p.GetDepartment() select new { Employee = e, Project = p };
Tips and Best PracticesTips and Best Practices
var good = from e in GetEmployees() let d = e.GetDepartment() from p in projects where d == p.Department select new { Employee = e, Project = p.Project };
var projects = (from p in GetProjects() select new { Project = p, Department = p.GetDepartment() }) .ToArray();
join
![Page 7: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/7.jpg)
Tips and Best PracticesTips and Best Practices
System.Collections.ArrayList arrayList;arrayList.Add(1);arrayList.Add("string");// Ew, where's the generics?
List<int> integers = list.OfType<int>().ToList();List<string> strings = list.OfType<string>().ToList();
System.Collections.ArrayList arrayList;arrayList.Add(1);arrayList.Add("string");// Ew, where's the generics?
List<int> integers = list.OfType<int>().ToList();List<string> strings = list.OfType<string>().ToList();
![Page 8: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/8.jpg)
Tips and Best PracticesTips and Best Practices
System.Collections.ArrayList arrayList;arrayList.Add(1);arrayList.Add(2);// But they’re all integers!
List<int> list = list.Cast<int>().ToList();
System.Collections.ArrayList arrayList;arrayList.Add(1);arrayList.Add(2);// But they’re all integers!
List<int> list = list.Cast<int>().ToList();
![Page 9: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/9.jpg)
Tips and Best PracticesTips and Best Practices• Aggregate
– Sum, Min, Max, Average, …• Set Operators
– Union, Intersect, Except, SelectMany, SequenceEqual…
• Morehttp://msdn.microsoft.com/en-us/vcsharp/
aa336746.aspx• Extension Methods
![Page 10: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/10.jpg)
DEMODEMOTips and Best Practices
![Page 11: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/11.jpg)
Expression TreeExpression Tree
• In-memory tree representation of LINQ Query
• Any LINQ Query node has it’s own type• XXXExpression classes• ExpressionType Enum• Interpreted to specific data source by
specific IQueryProvider like SQLQueryProvider
![Page 12: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/12.jpg)
Expression TreeExpression Tree
![Page 13: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/13.jpg)
DEMODEMOExpression Tree
![Page 14: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/14.jpg)
Enumerable vs. QueryableEnumerable vs. Queryable
• Both are static classes • Both contains extension methods• Enumerable extends IEnumarable<T>• Queryable extends IQueryable <T>
![Page 15: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/15.jpg)
Enumerable vs. QueryableEnumerable vs. Queryable
• Enumarable – Func<> delegate as method parameter– Intended for in-memory sequences iteration– Invokes the delegate as-is
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector);
![Page 16: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/16.jpg)
Enumerable vs. QueryableEnumerable vs. Queryable
• Queryable– Expression tree as method parameter– Expression tree Interpreted to specific data
source by IQueryProvider– There is no “real” delegate
public static IQueryable<TResult> Select<TSource, TResult>(this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector);
public static IQueryable<TResult> Select<TSource, TResult>(this IQueryable<TSource> source, Expression<Func<TSource, TResult>> selector);
![Page 17: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/17.jpg)
Enumerable vs. QueryableEnumerable vs. Queryable
• Queryable
C# 3.0 C
ompiler
C# 3.0 Query
IQueryProvider
T-S
QL
Sta
tem
ents
![Page 18: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/18.jpg)
DEMODEMOEnumerable vs. Queryable
![Page 19: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/19.jpg)
Compiled QueryCompiled Query
• Specific data source cached query (Like T-SQL)
• Saved in the application memory for reuse• Use CompiledQuery.Compile(…)
![Page 20: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/20.jpg)
DEMODEMOCompiled Query
![Page 21: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/21.jpg)
LINQ ToolsLINQ Tools
• Expression Tree Debugger Visualizer• SqlServer Query Debugger Visualizer• Paste XML as LINQ Add-In• Dynamic LINQ library• LINQPad• VLINQ – Visual LINQ Query Builder• Linqer – SQL to LINQ Converter
![Page 22: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/22.jpg)
DEMODEMOLINQ Tools
![Page 23: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/23.jpg)
NIH Is Bad!NIH Is Bad!
• LINQ to XSD• LINQ to Active Directory• LINQ to WMI• LINQ to Google / Ebay / Amazon…
• More:http://blogs.microsoft.co.il/blogs/vardi/
archive/2008/10/09/the-linq-list-projects.aspx
![Page 24: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/24.jpg)
Tips and Best PracticesTips and Best Practices
• Open the connection once for multiple DB queries
dbContext.Connection.Open;)(//queries
…
dbContext.Connection.Close;)(
dbContext.Connection.Open;)(//queries
…
dbContext.Connection.Close;)(
![Page 25: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/25.jpg)
DEMODEMOTypes of LINQ
![Page 26: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/26.jpg)
Q & AQ & A
![Page 27: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/27.jpg)
SummarySummary
• Using LINQ in more productivity• Improve LINQ performance
![Page 28: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/28.jpg)
Additional ResourcesAdditional Resources• Types of LINQ
http://blogs.microsoft.co.il/blogs/vardi/archive/2008/10/09/the-linq-list-projects.aspx
• 101 LINQ sampleshttp://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx
![Page 29: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/29.jpg)
Related SessionsRelated SessionsHardcore C#: Hidden Power and Flexibility'פבל יוסיפוביץ09:00-10:30 Galil Hall
Dynamic Languages and the .Net Frameworkשי פרידמן14:30-15:40 Tavor Hall
![Page 30: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/30.jpg)
![Page 31: Deep Dive into LINQ Eran Sharabi.NET Development Team Leader JohnBryce Training esharabi@johnbryce.co.il.](https://reader034.fdocuments.net/reader034/viewer/2022051515/551b0e515503465e7d8b5efd/html5/thumbnails/31.jpg)
© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.
The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after
the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.