
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. 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: Dim nw As New northwindEntities Dim res = From c In nw.Customers Where c.City = "Berlin" Order By c.ContactName Select c GridView1.DataSource = res But what if you wanted to retrieve Customer objects based on the purchases the customer had made? Dim res = From c In nw.Customers Join o In nw.Orders On c.CustomerID Equals o.CustomerID Where o.Freight > 0 Select c 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. 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, }; The result of this code will be an IEnumerable collection filled with anonymous objects. Now my list of tutorials will only contain those written by The Tallest.
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. 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. Anyhow, I forgot about this problem for a while until more recently when I started a new project where we were working with a lot of data in XML, and naturally, we turned to LINQ to XML... An Introduction to LINQ to XML This section gives a very brief introduction to the LINQ to XML API and its relationship to XPath. The LINQ to XML API provides methods for querying XML documents which are loaded into memory in the form of an XDocument (or X-Dom for short).