The LINQ Principle
Written by Mike James   
Thursday, 25 June 2020
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.

 csharp

To access the code for this project visit the 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.

Deep C#

 Buy Now From Amazon

DeepCsharp360

 Chapter List

  1. Why C#?

    I Strong Typing & Type Safety
  2. Strong Typing
       Extract 
    Why Strong Typing
  3. Value & Reference
  4.    Extract Value And Reference
  5. Structs & Classes
       Extract
    Structs & Classes 
  6. Inheritance
      
    Extract
    Inheritance
  7. Interfaces & Multiple Inheritance
      
    Extract Interface
  8. Controlling Inheritance
    II Casting & Generics
  9. Casting - The Escape From Strong Typing
      
    Extract Casting I ***NEW!
  10. Generics
  11. Advanced Generics
  12. Anonymous & Dynamic
    Typing
    III Functions
  13. Delegates
  14. Multicast Delegates
  15. Anonymous Methods, Lambdas & Closures
    IV Async
  16. Threading, Tasks & Locking
  17. The Invoke Pattern
  18. Async Await
  19. The Parallel For
    V Data - LINQ, XML & Regular Expressions
  20. The LINQ Principle
  21. XML
  22. LINQ To XML
  23. Regular Expressions
    VI Unsafe & Interop
  24. Interop
  25. COM
  26. Custom Attributes
  27. Bit Manipulation
  28. Advanced Structs
  29. Pointers 

Extra Material

 <ASIN:1871962714>

 <ASIN:B09FTLPTP9>

Banner


Deep C# - Casting the Escape from Strong Typing

Casting is one of the most confusing aspects of any modern language and it often makes beginners think hard. But if you know why you are doing it, then the how makes a lot more sense. We have encounte [ ... ]



Deep C# - Interface

Interfaces - what are they for? Not quite inheritance yet they seem to fit the same purpose. Find out in this extract from my new book, Deep C#: Dive Into Modern C#.


Other Articles

raspberry pi books

 

Comments




or email your comment to: comments@i-programmer.info

 

 



Last Updated ( Thursday, 25 June 2020 )