My name is Edward Tanguay. I'm an American software and web developer living and working in Berlin, Germany.
Book Notes posted on Saturday, February 28, 2009 permalink
Professional LINQ
by Scott Klein
I set up a LINQ-to-SQL WPF application and realized how simple LINQ is going to make data access so needed to get deeper into it. This is a fair book so far for the introduction to LINQ, just going through the basics up to now but you have to do that just to get into the new syntax. Some promising chapters coming up.
Preface
  1. "LINQ introduces queries as a first-class language construct."
  2. You can download the code for this book here.
Chapter 1: Project LINQ
  1. Shows verbose code to access store procedure.
  2. Also shows how awkward it is to read and write XML.
  3. Shows a couple of simple LINQ examples, nice.
  4. Has an example with the AdventureWorks database but doesn't show how to set it up, downloaded code gets an error, I posted a question about this at StackOverflow.
  5. Then does some LINQ to XML.
  6. "If you have ever used, and disliked, the DOM, you will love LINQ to XML."
  7. Lots of little typos in this book, e.g. on one page.
    var s - x.ToString();
    ...
    textbox1.Text = rToString();
  8. Many good examples in this chapter.
Chapter 2: a Look At Visual Studio 2008
  1. Talks about history of Visual Studio.
  2. Talks about differences between c# and vb.net.
  3. Shows simple example of extension method.
  4. Talks about lamba expressions.
  5. NOTE: Lambda expressions and anonymous methods are similar, except for the fact that lambda expressions are much more flexible and provide a more succinct syntax than anonymous methods.
  6. Shows conversion of lambda to regulare LINQ syntax, but nothing more.
Chapter 3: LINQ Queries
  1. Before linq, if you wanted to query an XML document, you need to learn the XQuery query language or XPath as well.
  2. Talks about how a query works internally.
  3. Talks about deferred execution.
  4. Compares to sql syntax.
  5. Shows how to remap the names, also with select new {...}.
  6. Discussus var and IEnumerable: basically you have to use var if you have select new {...} since that type is anonymous and doesn't exist yet.
  7. NOTE: Moving back and forth among data items is not possible with IEnumerable.
  8. The best time to use var is when a variable is initialized with an anonymous type.
  9. Talks about IQueryable.
  10. "It is recommended that you use query syntax whenever possible simply because it is easier to read, understand, and maintain."
  11. Talks about when to use query syntax and method syntax.
  12. Then does a project to summarize, but in windows forms.
Chapter 4: LINQ Standard Query Operators
  1. FLASHCARD: What is the difference between IQueryable and IEnumerable?
  2. Does SelectMany, OrderBy, etc.
  3. Uses both query and method syntax.
  4. thenby is interesting:
    IEnumerable<string> query =
        from c in contact
        where c.FirstName.StartsWith("S")
        orderby c.LastName
        thenby c.FirstName
        select new {c.FirstName, c.LastName, c.EmailAddress}
  5. Reverse() is nice:
    string[] reversednames = names.Reverse().ToArray();
  6. Talks about joins, group joins, grouping.
  7. Count, average, min, max, sum, distinct.
  8. Also union to combine two arrays.
  9. Intersect is useful.
  10. Talks about Empty (provides empty set if condition not met).
  11. Talks about Enumerable.Range and Enumerable.Repeat, nice.
  12. Also Cast if you are using ArrayList.
  13. Also OfType will pick e.g. integers out of an arraylist of strings and integers, nice.
  14. ToArray, ToDictionary and ToList, ToLookup.
  15. DefaultIfEmpty interesting, couldn't get it to work in simple example though from book.
  16. did First() and Last():
                var customers = from c in _db.Customers
                                where c.ContactName.StartsWith("S")
                                select c;
                TheListView.Items.Add(customers.First());
  17. Interesting: FirstOrDefault and LastOrDefault.
  18. Also Single.
  19. All() to see if all match a criteria:
    friends.All(name => name.Name.StartsWith("J"));
  20. Any().
  21. Contains.
                var customers = from c in _db.Customers
                                where c.Country == "Germany"
                                && c.ContactName.Contains("am")
                                select c;
  22. Also: Skip, SkipWhile.
  23. Then has windows form example of these.
Chapter 5: Understanding LINQ To XML
  1. shows basic examples of creating XML text from LINQ, e.g.:
                XDocument xmldoc = new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XComment("These are all the customers transfered from the database."),
                    new XElement("Customers",
                        new XElement("Customer",
                            new XAttribute("ID", 23),
                            new XElement("FullName", "Jim Tester"),
                            new XElement("Title", "Developer"),
                            new XElement("Company", "Apple Inc.")
                            ),
                        new XElement("Customer",
                            new XElement("FullName", "John Testly"),
                            new XElement("Title", "Tester"),
                            new XElement("Company", "Google")
                            )
                        )
                    );

                TheTextBlock.Text = xmldoc.Declaration.ToString() + Environment.NewLine + xmldoc.ToString();
  2. Section: LINQ to XML Programming Concepts.
  3. shows how to load XML from file and string, e.g.:
                TextReader tr = new StringReader(@"
    <Customers>
        <Customer>
        <FullName>Pat Newton</FullName>
        <Title>Salesperson</Title>
        <Company>Yahoo</Company>
        </Customer>
        <Customer>
        <FullName>Jim Newton</FullName>
        <Title>Google</Title>
        <Company>Microsoft</Company>
        </Customer>
        <Customer>
        <FullName>Angie Thompson</FullName>
        <Title>Developer</Title>
        <Company>Sun</Company>
        </Customer>
    </Customers>");
                XDocument xmldoc = XDocument.Load(tr);
  4. "One great thing about LINQ to XML in the .NET Framework is that indentation is automatically done for you, another great thing is the capability to easily make changes to the XML tree."
  5. Shows how to insert with AddAfterSelf.
  6. Doesn't show the obvious task of choosing an element by id, so asked about it at StackOverflow.
  7. Compares LINQ to XML to other technologies.
  8. FLASHCARD: How is LINQ-to-XML better than DOM?
  9. FLASHCARD: When use LINQ-to-XML and when XmlReader?
  10. "LINQ to XML, however, overcomes all of the XSLT shortcomings."
  11. MSXML is COM-based and, therefore, not recommended for use in managed code.
Chapter 6: Programming with LINQ To XML
  1. Creates an XML tree in C# just like he did in the last chapter, nothing new actually.
  2. shows XML literals in Visual Basic, wow, very easy:
  3. You preserve whitespace with the true property.
    customers = XElement.Load(@"C:\test\customers.xml", true);
  4. Shows how to load an XmlDocument into a XElement.
  5. shows how to select, e.g.:
                XElement element2 = xmldoc.Element("Customers").Elements("Customer").ElementAt(1);
                element2.Remove();
  6. shows you can put nodes in a foreach loop, nice:
    foreach (XElement employee in employees.Elements("Employee")
        textbox1.Text += employee;
  7. Talks about modifying and reshaping XML trees.
  8. how to save:
                xmldoc.Save(@"c:\temp\test234.xml");
    -talks about namespaces
    Chapter 7: LINQ To XML and Other LINQ Data Models
    I'm currently reading at this point. Review will continue soon...