|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Contents
Introduction.NET 3.0 has now been released, so we should all know it by now shouldn't we? Jeez, it doesn't seem like that long ago that .NET 2.0 came along. Well for those that don't realize .NET 3.0 actually contains quite a lot of new stuff, such as:
So as you can see there is a lot to be learned right there. I'm in the process of learning WPF/WCF but I am also interested in a little gem called LINQ, that I believe will be part of .NET 3.5 and Visual Studio "Orcas" (as its known now). LINQ will add new features to both C# and VB.NET. LINQ has three flavours:
LINQ is pretty cool, and I have been looking into it as of late, so I thought I would write an article about what I have learned in the LINQ/DLINQ/XLINQ areas, in the hopes that it may just help some of you good folk. This article will be focussed on LINQ, and is the first in a series of 3 proposed articles. The proposed article series content will be as follows:
But What Is LINQ Anyway?Well standard LINQ is a new addition to .NET (it adds more dlls basically) that allows the programmer to query inline data as they probably would be used to doing with standard SQL-type syntax. So where they may have had a query in a database or a SQL query string something like: SELECT * from Books WHERE QuatityInStock > 50 AND Price > 50.00
we would now write the following into as a valid LINQ query (assuming we have the relevant in memory data structure to support the query)
var result =
from b Books
where b.QuatityInStock > 50 AND Price > 50.00
select b;
See how similar this is. It's very powerful. So thats basically what LINQ allows us to do. And as one can imagine, DLINQ does similar stuff but with database objects, and XLINQ does queries/creation over XML documents. LINQ also introduces lot of concepts that have really come from other functional programming languages, such as Haskell, LISP. Some of these new concepts are:
These will hopefully become more familiar to you as we continue. PrerequisitesTo run the code supplied with this article you will need to install the May 2006 LINQ CTP which is available here, there is a new March 2007 CTP available, but its about 4GB for the full install (as its not just LINQ but the entire next generation of Visual Studio codenamed "Orcas") and quite fiddly, and probably going to change anyway, so the May 2006 LINQ CTP will be OK for the purpose of what this article is trying to demonstrate. Other Interesting ReadingsThere are a number of interesting sources for LINQ and functional programming concepts. There is obviously the LINQ site and also some nice web examples, and also some other articles right here at Code Project. I'll list a few for those of you that are curious enough, and want more to look at:
What This Article Is NotI've now told you where to download LINQ, and pointed you at some other LINQ resources and further readings (which I urge you to do) so by now you are probably thinking "what's left to discuss?" Well the honest answer is that this article's content could probably all be found quite easily using the other resources shown above. But you never know, this article just might put a new spin on things, and help you to understand LINQ in a different way, as each person has a different writing style, so too, does each person have a different learning style. Some folk just may like this article. And to be honest I quite enjoy writing articles, so I'll continue in the hope that someone will like this article's contents. I do, however, want people to know (just so people know that I am not selling myself as a purveyor of new knowledge), that all the information in this article is neither novel or really original, it can all be found easily using the web or by trawling the LINQ documentation. But sometimes it's nice to let someone else go through the learning for you and to learn from what they learned. See it as my journey into learning LINQ, which I am sharing with you here. Although to be honest it's not really standard LINQ that excites me it's DLINQ/XLINQ, for which there is not so much freely available information. So that really is a case of trawling the documentation. But fear not, that is what I will be doing for you good folk in the next two articles. So stay tuned for those future articles. It just would not have made sense to write about those two without some sort of words about standard LINQ. Still Interested?If (Yes==UserResponse)
{
SELECT RestOfArticleContent
}
A Little Word About The Attached demo ProjectBefore I delve into the nitty-gritty of LINQ, I would just like to mention a bit about the provided demo application. It looks like the figure shown below.
As you can see it comprises a left panel and a right area. On the left the user is able to view a PropertyGrid and a Numeric Up / Down control for each of the source Lists. Where the user is able to use the Numeric Up / Down to examine the individual query source
The main data query sources used for most queries will be simple based on _itemList = new List<Item> {
{ ItemID = 1, ItemName = "Enclopedia",
Category="Knowledge", UnitPrice = 55.99M, UnitsInStock = 39 },
{ ItemID = 2, ItemName = "Trainers",
Category="Sports", UnitPrice = 75.00M, UnitsInStock = 17 },
{ ItemID = 3, ItemName = "Box of CDs",
Category="Storage", UnitPrice = 4.99M, UnitsInStock = 13 },
{ ItemID = 4, ItemName = "Tomatoe ketchup",
Category="Food", UnitPrice = 0.56M, UnitsInStock = 53 },
{ ItemID = 5, ItemName = "IPod",
Category="Entertainment", UnitPrice = 220.99M, UnitsInStock
= 0 },
{ ItemID = 6, ItemName = "Rammstein CD",
Category="Entertainment", UnitPrice = 7.99M, UnitsInStock =
120 },
{ ItemID = 7, ItemName = "War of the worlds DVD",
Category="Entertainment", UnitPrice = 6.99M, UnitsInStock =
15 },
{ ItemID = 8, ItemName = "Cranberry Sauce",
Category="Food", UnitPrice = 0.89M, UnitsInStock = 6 },
{ ItemID = 9, ItemName = "Rice steamer",
Category="Food", UnitPrice = 13.00M, UnitsInStock = 29 },
{ ItemID = 10, ItemName = "Bunch of grapes",
Category="Food", UnitPrice = 1.19M, UnitsInStock = 4 }};
_orderList = new List<Order> {
{ OrderID = 1, OrderName = "John Smith", OrderDate =
DateTime.Now },
{ OrderID = 2, OrderName = "Professor X", OrderDate =
DateTime.Now },
{ OrderID = 3, OrderName = "Naomi Campbell", OrderDate =
DateTime.Now },
{ OrderID = 4, OrderName = "The Hulk", OrderDate =
DateTime.Now },
{ OrderID = 5, OrderName = "Malcolm X", OrderDate =
DateTime.Now }};
So it can be seen that the 1st using System;
using System.Collections.Generic;
using System.Text;
namespace LinqApp
{
public class Item
{
#region Instance fields
private int itemID;
private string itemName;
private string category;
private decimal unitPrice;
private int unitsInStock;
#endregion
#region Ctor
/// <summary>
/// ctor
/// </summary>
public Item()
{
}
#endregion
#region Public Properties
public int ItemID
{
get { return itemID;}
set { itemID = value; }
}
public string ItemName
{
get { return itemName;}
set { itemName = value; }
}
public string Category
{
get { return category;}
set { category = value; }
}
public decimal UnitPrice
{
get { return unitPrice;}
set { unitPrice = value; }
}
public int UnitsInStock
{
get { return unitsInStock;}
set { unitsInStock = value; }
}
public string ToString()
{
return "ItemID :" + ItemID + "\r\n" +
"ItemName :" + ItemName + "\r\n" +
"Category :" + Category + "\r\n" +
"UnitPrice :" + UnitPrice + "\r\n" +
"UnitsInStock :" + UnitsInStock;
}
#endregion
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace LinqApp
{
public class Order
{
#region Instance fields
private int orderID;
private string orderName;
private DateTime orderDate;
#endregion
#region Ctor
/// <summary>
/// ctor
/// </summary>
public Order()
{
}
#endregion
#region Public Properties
public int OrderID
{
get { return orderID;}
set { orderID = value; }
}
public string OrderName
{
get { return orderName;}
set { orderName = value; }
}
public DateTime OrderDate
{
get { return orderDate;}
set { orderDate = value; }
}
public string ToString()
{
return "OrderID :" + OrderID + "\r\n" +
"OrderName :" + OrderName + "\r\n" +
"OrderDate :" + OrderDate;
}
#endregion
}
}
The right hand area of the demo application shows the current query (actual LINQ syntax) and the results obtained.
This is pretty much how all the source data for the demo application is done, there may be some exceptions, where simple arrays of values are used instead of Think of the demo app as a mini LINQ playground. So that's about all I think you'll need to know about the demo app, for the moment, so shall we continue? OK Lets Crack On ThenAs I previously stated standard LINQ (not DLINQ / XLINQ) operates on in memory data structures such as arrays, collections etc etc. LINQ actually does this using methods known as Standard Query Operators. The new .NET 3.0 The majority of the Standard Query Operators are extension methods that extend I think the best way to tackle this subject is to introduce the LINQ Standard Query Operators. And give you a formal definition and an example of each one. The LINQ specification details the following operators:
Where the Standard Query Operators operate on sequences. Any object that implements the interface So you can see it's quite some beast. So what I'll attempt to do is give one formal definition and one example for each of the operators. I'll leave further reading (as I am showing one example, there are many possibilities for each operator) as an exercise for the reader. Operator InstructionsTime for some examples. Restriction (WHERE) OperatorThe Restriction operator filters a sequence based on a predicate. public static IEnumerable<T> Where<T>(
this IEnumerable<T> source,
Func<T, bool> predicate);
public static IEnumerable<T> Where<T>(
this IEnumerable<T> source,
Func<T, int, bool> predicate);
PredicatesWhat's a predicate you say. Well Wikipedia says: "In formal semantics a predicate is an expression of the semantic type of sets. An equivalent formulation is that they are thought of as indicator functions of sets, i.e. functions from an entity to a truth value. In predicate logic, a predicate can take the role as either a property or a relation between entities." And the LINQ Standard Query Operators documentation says: "The example below declares a local variable predicate of a delegate type that takes a Customer and returns bool. The local variable is assigned an anonymous method that returns true if the given customer is located in London. The delegate referenced by predicate is subsequently used to find all the customers in London." Func<Customer, bool> predicate = c => c.City == "London";
IEnumerable<Customer> customersInLondon = customers.Where(predicate);
So let's consider this simple example. What we would end up with is an expression that would be something like: IEnumerable<Customer> customersInLondon = customers.Where(c => c.City
== "London");
which could also be written in a more conventional way (that those more familiar with SQL would probably like) IEnumerable<Customer> customersInLondon =
from c in customers
where c.City == "London"
select c;
we could even be lazier than this, and instead of writing: IEnumerable<Customer> customersInLondon =
from c in customers
where c.City == "London"
select c;
we could write: var customersInLondon =
from c in customers
where c.City == "London"
select c;
That's how LINQ uses predicates. Basically the easiest way of thinking about what predicates are is to think about them as filters, that will evaluate to True or False, and as such will filter the But we digress. We needed to do this once, so that Predicates could be explained. But now that you've all got the hang of that we'll revisit the 1st Standard Query Operator. Restriction (WHERE) Operator RevisitedRecall the Restriction (Where) Query Operator was defined as: public static IEnumerable<T> Where<T>(
this IEnumerable<T> source,
Func<T, bool> predicate);
public static IEnumerable<T> Where<T>(
this IEnumerable<T> source,
Func<T, int, bool> predicate);
The Restriction Query operator can be of either of the forms shown above, where the the first argument of the predicate function represents the element to test. The second argument, if present, represents the zero-based index of the element within the source sequence. So let's see a real world example (using the attached demo project and the var iSmall =
from it in _itemList
where it.UnitPrice < 50.00M
select it;
A Little Word About "Var"One thing that is of interest here, which is the use of the Consider this statement: "It is also not required to declare type of query variable, because type inference automatically deduces the type when the var keyword is used." Concepts behind the C# 3.0 language, Tomas Petricek. What do we think of this? Well it's certainly better that what VB used to do, which was to determine the type at runtime. What LINQ does it to determine the type at compile time. So used wisely the EXAMPLE 1IEnumerable<Item> iSmall =
from it in _itemList
where it.UnitPrice < 50.00M
select it;
Instead of: EXAMPLE 2var iSmall =
from it in _itemList
where it.UnitPrice < 50.00M
select it;
Can you see the difference? In the 1st case, we were actually selecting the result type, and it which happens to be of What would you guess the query result type be for this: from c in customers
join o in orders on c.CustomerID equals o.CustomerID into co
from o in co.DefaultIfEmpty(emptyOrder)
select new { c.Name, o.OrderDate, o.Total };
or how about this one: from c in customers
select new
{
c.CompanyName, YearGroups =
from o in c.Orders
group o by o.OrderDate.Year into yg
select new
{
Year = yg.Key, MonthGroups =
from o in yg
group o by o.OrderDate.Month into mg
select new
{
Month = mg.Key, Orders = mg
}
}
};
See, it gets tricky. We all know typing is good and is our friend. But sometimes it can also be fairly complicated as well. In the 2nd example above, we simply use It is personal preference, but Projection (SELECT) OperatorThe Projection operator performs a projection over a sequence. public static IEnumerable<S> Select<T, S>(
this IEnumerable<T> source,
Func<T, S> selector);
public static IEnumerable<S> Select<T, S>(
this IEnumerable<T> source,
Func<T, int, S> selector);
The Projection Query operator can be of either of the forms shown above, where the first argument of the selector function represents the element to process. The second argument, if present, represents the zero based index of the element within the source sequence. So lets see a real world example (using the attached demo project and the var iNames = from i in _itemList select i.ItemName;
And maybe another example: var namesAndPrices =
_itemList.Where(i => i.UnitPrice >= 10).Select(i => new { i.ItemName,
i.UnitPrice }).ToList();
This is an interesting statement. The 1st part is the predicate There is also SelectMany, which I have not included here. But after you install LINQ, you can play with this yourself. Partitioning (Take / Skip) OperatorThe Partitioning operator is made up of four parts: Takepublic static IEnumerable<T> Take<T>(this IEnumerable<T> source,int count);
So let's see a real world example (using the attached demo project and the var MostExpensive2 = _itemList.OrderByDescending(i => i.UnitPrice).Take(2);
Where this example gets the two most expensive Items. Skippublic static IEnumerable<T> Skip<T>(
this IEnumerable<T> source,int count);
So let's see a real world example (using the attached demo project and the var AllButMostExpensive2 =
_itemList.OrderByDescending(i => i.UnitPrice).Skip(2);
Where this example gets all but the two most expensive Items. TakeWhileThe SkipWhileThe Join OperatorThe Joinpublic static IEnumerable<V> Join<T, U, K, V>(
this IEnumerable<T> outer,
IEnumerable<U> inner,
Func<T, K> outerKeySelector,
Func<U, K>> innerKeySelector,
Func<T, U, V> resultSelector);
It looks fairly nasty, but breaking it down a bit, it's really just saying get an outer data source, get an inner data source, get in outer key, get an inner key, and get the resultSet. So let's see a real world example (using the attached demo project and the var itemOrders =
from i in _itemList
join o in _orderList on i.ItemID equals o.OrderID
select new { i.ItemName, o.OrderName };
Where this example gets all Order objects that have the same OrderID value as that of the current Item.ItemID. JoinGroupThe Concatenation OperatorThe Concat operator concatenates two sequences. public static IEnumerable<T> Concat<T>(
this IEnumerable<T> first,
IEnumerable<T> second);
So lets see a real world example (using the attached demo project and the var items = ( from itEnt in _itemList
where itEnt.Category.Equals("Entertainment")
select itEnt.ItemName
).Concat
(
from it2 in _itemList
where it2.Category.Equals("Food")
select it2.ItemName
).Distinct();
Where this example gets all Item objects which have a Category of "Entertainment" and Concatenates that with the result of all Item objects which have a Category of "Food" and ensures there are no duplicates, by using the Order (OrderBy / ThenBy) OperatorThe public static OrderedSequence<T> OrderBy<T, K>(
this IEnumerable<T> source,
Func<T, K> keySelector);
public static OrderedSequence<T> OrderBy<T, K>(
this IEnumerable<T> source,
Func<T, K> keySelector, IComparer<K> comparer);
public static OrderedSequence<T> OrderByDescending<T, K>(
this IEnumerable<T> source,
Func<T, K> keySelector);
public static OrderedSequence<T> OrderByDescending<T, K>(
this IEnumerable<T> source,
Func<T, K> keySelector,
IComparer<K> comparer);
public static OrderedSequence<T> ThenBy<T, K>(
this OrderedSequence<T> source,
Func<T, K> keySelector);
public static OrderedSequence<T> ThenBy<T, K>(
this OrderedSequence<T> source,
Func<T, K> keySelector,
IComparer<K> comparer);
public static OrderedSequence<T> ThenByDescending<T, K>(
this OrderedSequence<T> source,
Func<T, K> keySelector);
public static OrderedSequence<T> ThenByDescending<T, K>(
this OrderedSequence<T> source,
Func<T, K> keySelector,
IComparer<K> comparer);
So let's see a real world example (using the attached demo project and the var orderItems =
_itemList.OrderBy(i => i.Category).ThenByDescending(i => i.UnitPrice);
Where this example gets all Items objects and simply order them 1st by Category and then by UnitPrice. Group (GroupBy) OperatorThe public static IEnumerable<IGrouping<K, T>> GroupBy<T, K>(
this IEnumerable<T> source,
Func<T, K> keySelector);
public static IEnumerable<IGrouping<K, T>> GroupBy<T, K>(
this IEnumerable<T> source,
Func<T, K> keySelector,
IEqualityComparer<K> comparer);
public static IEnumerable<IGrouping<K, E>> GroupBy<T, K, E>(
this IEnumerable<T> source,
Func<T, K> keySelector,
Func<T, E> elementSelector);
public static IEnumerable<IGrouping<K, E>> GroupBy<T, K, E>(
this IEnumerable<T> source,
Func<T, K> keySelector,
Func<T, E> elementSelector,
IEqualityComparer<K> comparer);
public interface IGrouping<K, T> : IEnumerable<T>
{
K Key { get; }
}
So lets see a real world example (using the attached demo project and the var itemNamesByCategory =
from i in _itemList
group i by i.Category into g
select new { Category = g.Key, Items = g };
NOTE: This example is quite different from those supplied with the LINQ CTP, and I could not get those to work, as I think the syntax may have changed since Microsoft wrote the LINQ documentation (for example GroupBy did not seem to liked, at least not how they described for this specfic query). The example above does actually work. This example gets all Category : Knowledge Enclopedia Category : Sports Trainers Category : Storage Box of CDs Category : Food Tomatoe ketchup Cranberry Sauce Rice steamer Bunch of grapes Category : Entertainment IPod Rammstein CD War of the worlds DVD It can be seen that we have all the Set (Distinct / Union / Intersect / Except) OperatorsThe DistinctThe public static IEnumerable<T> Distinct<T>(
this IEnumerable<T> source);
So let's see a real world example (using the attached demo project and the var itemCategory = (from i in _itemList select i.Category).Distinct()
Where this example gets the a unique list of all the Categories for the Items Union The public static IEnumerable<T> Union<T>(
this IEnumerable<T> first,
IEnumerable<T> second);
So let's see a real world example (using the attached demo project and the var un = (from i in _itemList select i.ItemName).Distinct()
.Union((from o in _orderList select o.OrderName).Distinct());
Where this example gets a unique IntersectThe public static IEnumerable<T> Intersect<T>(
this IEnumerable<T> first,
IEnumerable<T> second);
So lets see a real world example (using the attached demo project and the var inter = (from i in _itemList select i.ItemID).Distinct()
.Intersect((from o in _orderList select o.OrderID).Distinct());
Where this example gets a unique ExceptThe public static IEnumerable<T> Except<T>(
this IEnumerable<T> first,
IEnumerable<T> second);
So let's see a real world example (using the attached demo project and the var inter = (from i in _itemList select i.ItemID).Distinct()
.Intersect((from o in _orderList select o.OrderID).Distinct());
Where this example gets a unique Conversion (ToSequence / ToArray / ToList / ... ) OperatorsThe ToSequenceThe public static IEnumerable<T> ToSequence<T>(
this IEnumerable<T> source);
So let's see a real world example (using the attached demo project and the _itemList var query = _itemList.ToSequence().Where(i => i.Category.Equals(
"Entertainment"));
Where this example takes a ToArrayThe public static T[] ToArray<T>(
this IEnumerable<T> source);
So let's see a real world example (using the attached demo project and the var query = _itemList.ToArray().Where(i => i.Category.Equals("Food"));
Where this example takes a ToListThe public static List<T> ToList<T>(
this IEnumerable<T> source);
So let's see a real world example (using the attached demo project and the var query = _itemList.ToList().Where(i => i.ItemID > 5).Reverse();
Where this example takes a source ToDictionaryThe public static Dictionary<K, T> ToDictionary<T, K>(
this IEnumerable<T> source,
Func<T, K> keySelector);
public static Dictionary<K, T> ToDictionary<T, K>(
this IEnumerable<T> source,
Func<T, K> keySelector,
IEqualityComparer<K> comparer);
public static Dictionary<K, E> ToDictionary<T, K, E>(
this IEnumerable<T> source,
Func<T, K> keySelector,
Func<T, E> elementSelector);
public static Dictionary<K, E> ToDictionary<T, K, E>(
this IEnumerable<T> source,
Func<T, K> keySelector,
Func<T, E> elementSelector,
IEqualityComparer<K> comparer);
So let's see a real world example (using the attached demo project where a simple 2D array is used) var scoreRecords = new [] { new {Name = "Alice", Score = 50},
new {Name = "Bob" , Score = 40},
new {Name = "Cathy", Score = 45}
};
var scoreRecordsDict = scoreRecords.ToDictionary(sr => sr.Name);
Where this example creates a new 2D Array and converts that to a key/value ToLookupThe OfTypeThe public static IEnumerable<T> OfType<T>(
this IEnumerable source);
So let's see a real world example (using the attached demo project where a simple array is used) object[] numbers = { null, 1.0, "two", 3, 4.0f, 5, "six", 7.0 };
var doubles = numbers.OfType<double>();
Where this example creates a new Array of varying objects, and then the CastThe Equal OperatorThe EqualAll operator checks whether two sequences are equal. public static bool EqualAll<T>(
this IEnumerable<T> first,
IEnumerable<T> second);
So let's see a real world example (using the attached demo project and the This is a Not Equal Example: var eq = (from i in _itemList select i.ItemID).Distinct().EqualAll((
from o in _orderList select o.OrderID).Distinct());
Where this example gets all This is an Equal Example: int[] scoreRecords1 = new [] { 10,20 };
int[] scoreRecords2 = new [] { 10,20 };
var eq2 = scoreRecords1.EqualAll(scoreRecords2);
Where this example creates two new arrays, which have the same elements, and as such when they are compared using the Element (First / FirstOrDefault / ... ) OperatorsThe Set operators are made up of nine parts: FirstThe public static T First<T>(
this IEnumerable<T> source);
public static T First<T>(
this IEnumerable<T> source,
Func<T, bool> predicate);
The So let's see a real world example (using the attached demo project and the string itemName = "War of the worlds DVD";
Item itm = _itemList.First(i => i.ItemName == itemName);
Where this example takes the 1st element of the FirstOrDefaultThe public static T FirstOrDefault<T>(
this IEnumerable<T> source);
public static T FirstOrDefault<T>(
this IEnumerable<T> source,
Func<T, bool> predicate);
The So let's see a real world example (using the attached demo project and the string itemName = "A Non existence Element";
Item itm = _itemList.FirstOrDefault(i => i.ItemName == itemName);
Where this example takes the 1st or default element of the LastThe public static T Last<T>(
this IEnumerable<T> source);
public static T Last<T>(
this IEnumerable<T> source,
Func<T, bool> predicate);
The This works the same way as First, I'll leave this as an excercise for the reader. LastOrDefaultThe public static T LastOrDefault<T>(
this IEnumerable<T> source);
public static T LastOrDefault<T>(
this IEnumerable<T> source,
Func<T, bool> predicate);
The This works the same way as SingleThe public static T Single<T>(
this IEnumerable<T> source);
public static T Single<T>(
this IEnumerable<T> source,
Func<T, bool> predicate);
The This works the same way as First, I'll leave this as an excercise for the reader. SingleOrDefaultThe public static T SingleOrDefault<T>(
this IEnumerable<T> source);
public static T SingleOrDefault<T>(
this IEnumerable<T> source,
Func<T, bool> predicate);
The This works the same way as ElementAtThe public static T First<T>(
this IEnumerable<T> source);
public static T First<T>(
this IEnumerable<T> source,
Func<T, bool> predicate);
The So let's see a real world example (using the attached demo project and the Item thirdMostExpensive =
_itemList.OrderByDescending(i => i.UnitPrice).ElementAt(2);
Where this example orders the source ElementAtOrDefaultThe public static T ElementAtOrDefault<T>(
this IEnumerable<T> source,
int index);
The So let's see a real world example (using the attached demo project and the Item itm = _itemList.ElementAtOrDefault(15);
Where this example simple attempts to fetch a non existent element from the DefaultIfEmptyThe public static IEnumerable<T> DefaultIfEmpty<T>(
this IEnumerable<T> source);
public static IEnumerable<T> DefaultIfEmpty<T>(
this IEnumerable<T> source,
T defaultValue);
The This works the same way as FirstOrDefault, I'll leave this as an excercise for the reader. Generation (Range / Repeat / Empty ) OperatorsThe Set operators are made up of three parts: RangeThe Range operator generates a sequence of integral numbers. public static IEnumerable<int> Range(
int start,
int count);
The Range operator allocates and returns an enumerable object that captures the arguments. So let's see a real world example (using the attached demo project) int[] squares = Sequence.Range(1, 10).Select(x => x * x).ToArray();
Where this example creates a new array of squared numbers (1 - 10) by using the takes the static RepeatThe Repeat operator generates a sequence by repeating a value a given number of times. public static IEnumerable<T> Repeat<T>(
T element,
int count);
The Repeat operator allocates and returns an enumerable object that captures the arguments. So let's see a real world example (using the attached demo project) int[] numbers = Sequence.Repeat(5, 5).ToArray();
Where this example creates a new array of 5 repeated 5s EmptyThe Repeat operator generates a sequence by repeating a value a given number of times. public static IEnumerable<T> Empty<T>();
The Empty operator caches a single empty sequence of the given type. When the object returned by Empty is enumerated, it yields nothing. Im not sure why you would want to do this so I'll leave this as an excercise for the reader. Quantifiers (Any / All / Contains ) OperatorsThe AnyThe public static bool Any<T>(
this IEnumerable<T> source);
public static bool Any<T>(
this IEnumerable<T> source,
Func<T, bool> predicate);
The So let's see a real world example (using the attached demo project) bool b = _itemList.Any(i => i.UnitPrice >= 400);
Where this example simply returns a AllThe public static bool All<T>(
this IEnumerable<T> source,
Func<T, bool> predicate);
The So let's see a real world example (using the attached demo project and the var itemNamesByCategory =
from i in _itemList
group i by i.Category into g
where g.All(i => i.UnitsInStock > 0)
select new { Category = g.Key, Items = g };
Where this example uses a ContainsThe public static bool Contains<T>(
this IEnumerable<T> source,
T value);
The So let's see a real world example (using the attached demo project and the bool b = _itemList.Contains(_itemList[0]);
Where this example simply returns a Aggregate (Count / LongCount / Sum / ... ) OperatorsThe CountThe public static int Count<T>(
this IEnumerable<T> source);
public static int Count<T>(
this IEnumerable<T> source,
Func<T, bool> predicate);
The So let's see a real world example (using the attached demo project and the var foodCat = (from i in _itemList
where i.Category.Equals("Food")
select i).Count();
Where this example simply returns a count of all the We must be quite carful with What I have done here is a query result, then called the LongCountThe public static long LongCount<T>(
this IEnumerable<T> source);
public static long LongCount<T>(
this IEnumerable<T> source,
Func<T, bool> predicate);
The Works the same as SumThe public static Numeric Sum(
this IEnumerable<Numeric> source);
public static Numeric Sum<T>(
this IEnumerable<T> source,
Func<T, Numeric> selector);
The So let's see a real world example (using the attached demo project and the var totals = (from i in _itemList select i.UnitPrice).Sum();
Where this example simply sums the MinThe public static Numeric Min(
this IEnumerable<Numeric> source);
public static T Min<T>(
this IEnumerable<T> source);
public static Numeric Min<T>(
this IEnumerable<T> source,
Func<T, Numeric> selector);
public static S Min<T, S>(
this IEnumerable<T> source,
Func<T, S> selector);
The So let's see a real world example (using the attached demo project and the var minimum = (from i in _itemList select i.UnitPrice).Min();
Where this example simply fetches minimum MaxThe Max operator finds the maximum of a sequence of numeric values. public static Numeric Max(
this IEnumerable<Numeric> source);
public static T Max<T>(
this IEnumerable<T> source);
public static Numeric Max<T>(
this IEnumerable<T> source,
Func<T, Numeric> selector);
public static S Max<T, S>(
this IEnumerable<T> source,
Func<T, S> selector);
The
AverageThe public static Result Average(
this IEnumerable<Numeric> source);
public static Result Average<T>(
this IEnumerable<T> source,
Func<T, Numeric> selector);
The
AggregateThe public static T Aggregate<T>(
this IEnumerable<T> source,
Func<T, T, T> func);
public static U Aggregate<T, U>(
this IEnumerable<T> source,
U seed,
Func<U, T, U> func);
The I have to say this one actually defeated me. I searched and searched for another example, as the LINQ documentation one is pretty dire. Check it out, this one is direct from LINQ documentation. var longestNamesByCategory =
products.
GroupBy(p => p.Category).
Select(g => new {
Category = g.Key,
LongestName =
g.Group.
Select(p => p.Name).
Aggregate((s, t) => t.Length > s.Length ? t : s)
});
This is not a very nice example is it. This is kind of what we are getting with LINQ. Its very powerful, but some of it is pure crazy syntax. I mean what the heck is this one above telling someone. It's not very clear to me. Even the fabulous 101 LINQ Samples doesnt list an FoldFolding is nice concept straight out of the functional programming world, it allows us to fold in a new function to elements of a list ( Let's have a look at one of these: double[] doubles = { 2,4,6,8,10 };
double product = doubles.Fold((runningProduct,
nextFactor) => runningProduct * nextFactor);
In this simple example, we have an array of which we want to get the product. We can simply use fold to literally fold some inline function (namely Well thats about it for the Standar Query Operators, if youve made it this far well done. It took me ages to write this, and it's probably taken you ages to read this. So I'll forgive you if you want to come back later. But the next bit is all about dynamically created (at runtime) queries. Up until now it's all been pre-compiled queries, which is all very well but not very realistic. In the real world we would want to do dynamic queries wouldn't we. Dynamically Creating LINQ QueriesSo far we have looked at static (defined at compile time) queries which is all very well but not really what we wI'll probably want to do for our real world applications. It is also possible to create LINQ queries programatically using information the user may have entered or selected from a UI. This may be achieved use of the following principles: By The Use Of VariablesWe can simply introduce variables in the query, as shown here: decimal priceVal = 50.00M;
//query will now use a variable so is dynamic
var iSmall = from it in _itemList
where it.UnitPrice < priceVal
select it;
So by introducing a simple variable, we can control the query. Quite simple. QueryExpression Type (Mainly Used In DLINQ)
If, however, we look in Visual Studio 2005 (assuming you have May 2006 LINQ CTP installed), we get a better picture.
It can be seen from this figure that we can literally provide any So we could actually do something like (paying special attention to the use of the filter, which can be any string) //where filter is a string which could be set to "city = 'London'"
string filter = "city = 'London'";
expression = QueryExpression.Where(expression,
QueryExpression.Lambda(filter, e));
//Finally, we create a new query based on that expression.
//where db inherits from DataContent and was automatically generated using
//the DLINQ tool Sqlmetal.exe
var query = db.CreateQuery<EmployeeView>(expression);
This example is actually a DLINQ query. But this should be possible in standard LINQ using the The InteractiveQuery project which is installed as part of the May 2006 CTP is a good place to look for dynamic DLINQ queries. IQuerablable InterfaceTo do runtime queries over in memory objects (LINQ) you WI'll need the new March 2007 CTP, which allows the user to do this by the use of the NEW The Wayward WebLog shows the following example to do this. And I recommend you read this. IQueryable q = ...;
ParameterExpression p = Expression.Parameter(typeof(Customer), "c");
LambdaExpression predicate =
QueryExpression.Lambda("c.City = 'London'", p);
Expression where = QueryExpression.Where(q.Expression, predicate);
q = q.CreateQuery(where);
I have since been in contact with Matt Warren author of The Wayward WebLog and he sent me a nice email (see I am trying to help you folks out). So I'll go through what he told me. If we imagine that we have a method like: IQueryable Between(IQueryable query, int min, int max)
{
return from n in query where n >= min && n <= max select n;
}
We now have the facilities to query an Suppose we had a simple array: int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
and that we then queried this array, like: IEnumerable<int> query =
from n in numbers
where n %2 == 1
select n;
OK, so now we have a query result, which is of type IQueryable<int> query2 = Between(query.AsQueryable(), 0, 10);
So what's going on here? Well we re-use the results of the 1st query, which yielded a I'm sure you'll agree we all have to learn how to do this at some stage. Personally I'm going to let the CTP mature a little more and the install instructions become a bit more clear (The current CTP is for the entire "Orcas" project, which is the next version of Visual Studio, so it's huge). But that's only my opinion, if you just cant wait for dynamic queries and a sneaky peak at "Orcas" then download the March 2007 CTP. That's ItWell that's actually about it. As I said this article has probably not shown you much that you could not have learned from going to 101 LINQ Samples however, all information must come from somewhere. And perhaps some folks would not have known about 101 LINQ Samples or even LINQ unless they actually read this article, in which case I've probably done them a favour. Also this article does show actual working code, where as I found that some of the LINQ samples in the LINQ documentation, just did not work, or were so complicated that they would scare some folk. I've really tried to keep the examples in this article as simple as possible. The next two articles (XLlNQ / DLINQ) should display some new material which should be of use to you all....I Promise I'll try and make them cover new material. So What Do You Think?I would just like to ask, if you liked the article please vote for it, as it allows me to know if the article was at the right level or not. Also, if you think that the next two proposed articles should include this much material or less material. Let me know, after all I want to write articles that actually help people out. I know this one had a lot of stuff in it. But it's new stuff, that is common for LINQ/DLINQ/XLINQ, so lots of information had to be covered. Anyway let me know your thoughts. ConclusionI have quite enjoyed constructing this article, and have been quite refreshed at just how easy LINQ is to actually use (well most of it, some of the Group and Aggregate operators are just plain nasty). I also had quite a nostalgic feeling as it reminded me of doing Haskell programming, which I would thoroughly recommend everyone avoid, as its simply crazy. But if you like lambdas, then you should get jiggy with curries and lazy evaluation and all that functional type of stuff. Its quite different actually. Historyv1.0 23/03/07: Initial issue Bibilography | ||||||||||||||||||||