WHAT'S NEW?
Loading...

Day 17 Programming in C# 70-483

Index


  • Introduction
  • Linq features
  • Linq queries
  • Linq under the hood
  • Linq to XML
  • References

Introduction

This post is part of a series of post to help you prepare the MCSD certification, particularly the certification exam 70-483, based on the book:


You will find all the code available in the following GitHub repository. Lets talk a little bit about threads and how they work using a .Net development environment.

Linq features

Linq aka Language-Integrated Query was introduced with C# v3.5 and it's a widely language used to query data. There are some features in the language that make Linq possible:
  • Implicitly typed variables: this happens whenever you define you variables using the keyword "var" instead of doing an explicit type definition. This is necessary using Linq because sometimes you can't specify the return explicitly because you don't know it. It makes you code more readable as well.
  • Object initialization syntax: it enables you to combine creating a new object and setting its properties in one statement and this is required when working with anonymous types.
  • Lambda expressions: before lambda expressions, it's necessary to have clear what anonymous methods are (delegates). They were introduced in C# v2.0 to create, assign and pass methods in one line. 
    • .Net introduced shorthand 
      • Func<T,T> and 
      • Action<...> (returns nothing) to simplify the creation process. 
    • Lambdas use the => notation which can be read as "becomes".
    • Linq often has to pass a Func<> delegate to a method.
  • Extension methods: Extend an existing type with new behavior without using inheritance. It uses the special "this" keyword to mark itself as an extension method. Linq is entirely based on extension methods.
  • Anonymous types: this is a mix between implicit typing and object initializes. You create an anonymous type by using "var" and the new operator without specifying a type. This is used in Linq when you create a projection, so you select certain properties from a query and form a specific type.

Linq queries

There are two ways to query data with Linq: Query syntax or Method syntax, ie:


Linq query operators: these are the different actions we can perform over our data and that we know is implemented in other providers like Linq to XML, Linq to entities, Linq to Objects. Here are some: All, Any, Average, GroupBy, Join (it's used to combine data from two or more sources by specifying the property that needs to be equal), Max, Min, OrderBy, Select, Skip, Take (skip/take are used to add pagination, ie: when you call a db and you want to split the answer in pages so performance is not affected), Where...

Other useful features are projection (select) and grouping (groupBy). Projection is used to select another type or an anonymous type as the result of your query. Grouping, you group your data by a certain property and then work with that result.

If you want to test your queries there's a nice tool called linqpad which can help you to create a playground where do your experiments. 

Linq under the hood


As you already know how extension methods is used in Linq you might want to change the behavior of one of the operations provided. Then you have to remove the using System.Linq statement and try with the following approach:


Here you'll see how an iterator pattern is implemented. By using "this" we are extending the behavior of our method. The "yield" parameter is used to return values but one at a time. This is called "deferred execution".

This is important when you work with other Linq providers like Linq to entities or linq to SQL (it parses the query and transform it to SQL). The query won't be sent to the database until the result is iterated over. Iterating can happen when you call ToList() or when you iterate over the results in a for each statement (which calls the MoveNext() method).

Linq to XML

In previous lessons we've already talked about how to access XML files. Here I want to show you another approach. XDocument class is used to load a file, it works with the XNode abstract class. You can also use XDocument.Descendants or XDocument.Elements as well as XDocument.Nodes to access the child of a node. All those elements grant you access to the attributes within a node.

It also allows you to create your own XML. By calling the Add() method you can start building your file or in another way, you can use the XElement constructor that takes an array of objects that form the content.

Finally, in order to update your XML files you have two approaches:
  1. procedural way: it basically consists on building your objects one by one and then attach them to the xml structure.
  2. functional construction: it consists on using LINQ to XML syntax to find the bits you want to modify and update them with the new values.
It depends how difficult is your transformation whether you use functional or procedural transformations, If the XML structure changes the functional has a lot more benefits.

0 comments:

Post a Comment