101 LINQ Samples. LINQ to Tree - A Generic Technique for Querying Tree-like Structures. Download source code - 60.78 KB Contents Overview This article looks at how LINQ can be applied to tree-like data, using LINQ to XML as an example. A generic approach to querying trees is constructed, which allows any tree structure to be made LINQ 'compatible' with the implementation of a simple adapter class. This idea is extended further with T4 templates for code generation, with examples of LINQ to VisualTree (WPF / Silverlight), LINQ to WinForms, and LINQ to Filesystem presented. Introduction One of my favorite features of the C# programming language is LINQ.
When I started learning WPF, I thought it might be useful to apply LINQ to the visual tree in order to query the state of the user interface (UI). After a quick Google search, I stumbled upon Peter McGrattan's LINQ to Visual Tree implementation, which simply flattens the tree into a 1-dimensional structure. An Introduction to LINQ to XML The nodes matched by each part of the query are highlighted in the XML document above. LINQ to CSV. To Think in LINQ -- Visual Studio Magazine. Practical .NET To Think in LINQ If you start "thinking in LINQ" you'll get more done with less code, and what you write will be simpler than using SQL.
Switching to LINQ and the Entity Framework (EF) means that code you'll write for your data-driven applications will be simpler than the equivalent SQL. I admit: You can't do everything with LINQ that you can with SQL (though, when you need to, you can execute SQL from the EF). But SQL doesn't generate objects from rows in tables, either. Here's a look at how to do the typical things you'll need to do in an application, but done with LINQ+EF with some hints about "thinking in LINQ" and a few traps to avoid. Retrieving Data Across Multiple Tables In an EF model based on the Northwind database, this code would retrieve all the Customers that have a home office in Berlin, sort them by the name of the contact person and display the result in a GridView: But if you're joining two tables, you probably want data from both tables. Introduction to LINQ - Simple XML Parsing | Switch on the Code. Every programmer has had to parse an XML file.
If you're trying to tell me you haven't, then you're lying and you just don't remember it. You probably shouldn't even call yourself a programmer. If you don't remember the history of reading an XML document, here's the Cliffs Notes version. In the beginning there was line-by-line parsing. It was hard, error prone, and usually sucked. It's the anonymous types and methods introduced into .NET 3.5 that makes LINQ possible. Let's start out this tutorial by looking at some XML code. <? First, we're going to use some simple LINQ syntax to create some anonymous objects with the properties, Author, Title, and Date. XDocument xmlDoc = XDocument.Load("TestFile.xml"); var tutorials = from tutorial in xmlDoc.Descendants("Tutorial") select new { Author = tutorial.Element("Author").Value, Title = tutorial.Element("Title").Value, Date = tutorial.Element("Date").Value, }; Now my list of tutorials will only contain those written by The Tallest.