The LINQ principle
Friday, 27 August 2010
Article Index
The LINQ principle
IEnumerable
Generic enumeration
Raw LINQ
More with LINQ

More Selection

Understanding how the query expression is converted into calls to the query extension methods greatly clarifies how LINQ works and what you can do with it.

If you want to stay with a clear design  you should always use Select as a “projection operator”, that is it should reduce the data item to something that is a subset of itself.

For example, if the data item is a struct then the select should specify which fields are to be extracted and returned in a “smaller” struct.  This is a common and interesting task so let’s look at another simple example of select.

First we need a class or struct to hold the data:

public struct Contact
{
public string name { get; set; }
public string address1 { get; set; }
public int phone { get; set; }
}

The default property implementations are just enough code to allow us to initialise a List of such structs directly:

List<Contact> AddressBook=
new List<Contact>(){
new Contact(){
name="mike",
address1="Anywhere1",
phone=123},
new Contact(){
name="Ian",
address1="Anywhere2",
phone=124},
new Contact(){
name="john",
address1="Anywhere3",
phone=125}
};

As List supports IEnumerator we can now move immediately to using LINQ to query it.

To extract a single field, the name say, you would use a query something like:

var q = from N in AddressBook
select N.name;
foreach ( var n in q)
{
MessageBox.Show(n);
}

Notice the use of var to avoid having to state the data type. If you don’t want to use an anonymous type then you can replace the var with string.

It is easy to see how this translates to the call to the select extension method which has to return just the single field as a string but how do you return multiple fields?

The problem is that you don’t have a data type, i.e. a struct, that holds a subset of fields.  You could define a suitable struct but the standard solution  is to create a new anonymous type on the fly:

var q = from N in AddressBook
select new { N.name, N.phone };
foreach ( var n in q)
{
MessageBox.Show(n.name.ToString +
n.phone.ToString());
}

You can see that these "dynamic" facilities are required to make LINQ look simple.

Other LINQs

Basically we have been looking at LINQ to objects but the same principles apply to all the other implementations - LINQ to SQL, XML and ADO for example.

You should now be in a position to see how these work even if you still need to dig out the details. What is even more impressive is that if you have a data source that LINQ doesn’t support you should be able to add it by implementing IEnumerable.

LINQ is just a powerful and compact way of working with data that many suggest that it should be used in place of alternative methods of processing. In other words use a LINQ expression in favour of a For each loop when ever possible.

This is another story and another article.

 

To access the code for this project, once you have registered,  click on CodeBin.

 

If you would like to suggest a topic for our Core C# section or if you have any comments contact our C# editor Mike James.

 

If you would like to be informed about new articles on I Programmer you can either follow us on Twitter, on Facebook or you can subscribe to our weekly newsletter.

Banner


XML in C# - Using XElement

.NET has some really easy-to-use facilities for creating and editing XML. Many of these facilities were introduced to make Linq to XML work better, but you can make use of them in more general situati [ ... ]



What's The Matter With Pointers?

Back in the days when C was the language of choice, pointers meant programming and vice versa. Now in the more sophisticated and abstract days of C#, and even C++, raw pointers are a facility that is  [ ... ]


Other Articles

<ASIN:1449380344>
<ASIN:0596159838>

<ASIN:1430229799>

<ASIN:0262201755>

<ASIN:0596800959>

<ASIN:047043452X>

<ASIN:193435645X>

<ASIN:0596007124>


Last Updated ( Friday, 27 August 2010 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.