Fabrice's weblog

Tools and Source

News

My .NET Toolbox
An error occured. See the script errors signaled by your web browser.
No tools selected yet
.NET tools by SharpToolbox.com

Read sample chapters or buy LINQ in Action now!
Our LINQ book is also available on AMAZON

.NET jobs

Emplois .NET

transatlantys hot news

Contact

Me

Others

Selected content

November 2008 - Posts

Speaking for the Communauté .NET Montréal next Monday

In addition to speaking at DevTeach Montreal next week, Frédéric Schäfer and I will also give a demonstration of LINQ, Entity Framework and WPF in action to the Communauté .NET Montréal next Monday. This will be in French.
A bientôt à Montréal et DevTeach !

 

PS: Why should you go to DevTeach?

Posted: Nov 24 2008, 12:28 AM by Fabrice Marguerie | with no comments
Filed under: ,
IQToolkit, building your own LINQ IQueryable provider

Matt Warren, who provides the most detailed HOWTO for creating your own LINQ IQueryable provider, has now published a dedicated toolkit on CodePlex:

IQToolkit is essential if you are building your own LINQ IQueryable provider. It contains common tools and source code you can apply to your own project.
In the toolkit you will find useful techniques for manipulating LINQ expression trees, implementing IQueryable providers, and a host of extensible components for building providers that target translation of LINQ expressions into SQL like languages.

http://www.codeplex.com/IQToolkit

Creating a LINQ provider is not a walk in the park, but at least if you have to, you have everything you need at hand.


Cross-posted from http://linqinaction.net
Using the Select LINQ query operator with indexes

Yesterday, Fred asked me if I could help him to convert C# code to LINQ. The solution may not obvious to find unless you know LINQ well. I will reproduce here the solution I gave Fred. Whether the LINQ version of the code is easier to read than the original one is arguable. The purpose here is more to show LINQ's Select query operator in action.

Here is the original code:

int CountCorrectChars(string proposedValue, string correctValue)
{
  int correctCount = 0;
  for (int i=0; i < proposedValue.Length && i < correctValue.Length; i++)
    if (proposedValue[i] == correctValue[i])
      correctCount++;
  return correctCount;
}

Here is the LINQ version that I suggested:

int CountCorrectChars(string proposedValue, string correctValue)
{  
return correctValue
    .Select((testChar, index) => new {Character=testChar, Index=index})
    .Count(testChar => (testChar.Index < proposedValue.Length)
&& (testChar.Character == proposedValue[testChar.Index]));
}

As you can see, the LINQ version is not so easy to understand and is verbose. Of course, we could use shorter names, but that wouldn't change the complexity of the query. The LINQ version is not as good in terms of performance either... So, should we use LINQ or not? My point here is that LINQ is not a "one size fits all" solution. You should use it wisely and avoid complexifying code by choosing always to use LINQ.

What's interesting in this example, is also simply the use of Select with a two-parameter lambda expression. You may know the version of Select that takes a single-parameter lambda well, but its counterpart is less known (and used).

This is something that we cover in LINQ in Action in section 4.4.2. Here is what we write there, which gives another example of Select in action:

The Select and SelectMany operators can be used to retrieve the index of each element in a sequence. Let’s say we want to display the index of each book in our collection before we sort them in alphabetical order:

index=3         Title=All your base are belong to us
index=4         Title=Bonjour mon Amour
index=2         Title=C# on Rails
index=0         Title=Funny Stories
index=1         Title=LINQ rules

Here is how to use Select to achieve that:

Listing 4.15    Code-behind for the first ASP.NET page    (SelectIndex.csproj)
var books =
  SampleData.Books
    .Select((book, index) => new { index, book.Title })
    .OrderBy(book => book.Title);
ObjectDumper.Write(books);

This time we can’t use the query expression syntax because the variant of the Select operator that provides the index has no equivalent in this syntax. Notice that this version of the Select method provides an index variable that we can use in our lambda expression (precision not in the book: its not the name "index" that is important. You can use another name if you want. What makes the difference is that the lambda expression takes two parameters). The compiler automatically determines which version of the Select operator we want to use just by looking at the presence or absence of the index parameter. Notice also that we call Select before OrderBy. This is important to get the indices before the books are sorted, not after.

...One more tool in your toolbox. Now, use it wisely.

Update: Mark Sowul suggests a simpler solution:

return correctValue.Where((testChar, index) => index < proposedValue.Length && testChar == proposedValue[index]).Count();
Somehow I missed that Where overload. 

Cross-posted from http://LinqInAction.net

See you at TechEd EMEA in Barcelona next week

I'll be at TechEd in Barcelona next week. I'll be at the Ask The Experts booths a few hours during the week. Feel free to come and say hello. I'll be available if you want to discuss about LINQ, the LINQ in Action book, WPF and Silverlight, my projects, or .NET in general :-)

Please ping me if you'll be there too and would like to meet.


Cross-posted from http://linqinaction.net
Posted: Nov 04 2008, 12:42 AM by Fabrice Marguerie | with no comments
Filed under:
More Posts