Geeks With Blogs

News Awarded Microsoft MVP C#.NET - 2007, 2008 and 2009

I am born in Bangladesh and currently live in Melbourne, Australia. I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net)and born in Bangladesh.
I am founder and Chief Executive Officer of
Simplexhub, a highly experienced software development company based in Melbourne Australia and Dhaka, Bangladesh. Co-founder and core developer of Pageflakes
Simplexhub, is on its mission to build a smart virtual community in Bangladesh and recently launched beta an ASP.NET MVC application written in C#.NET.

Some of My Articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET

Shahed Khan blog

Here is simple LINQ example where I have used Extension methods "Where", to select the participants whose score is greater than 80.

var participants =
.Where(participant=> participant.Score > 80)
.OrderByDescending(participant => parricipant.Score)
.Select(participant => new { participant.Id,
Name=participant.Name });

Note: here we are sending a Lamda expression (participant=>participant.Score>80) as parameter of the "Where" extension method. Ever wondered how this extension method is declared and what is happening inside?

Here you go:

public static IEnumerable<TSource> Where<TSource>(
this IEnumerable<TSource> source,
Func<TSource, Boolean> predicate)
  foreach (TSource element in source)
    if (predicate(element))
    yield return element;

I am sure you noticed the "this" word in the above code snippet, that's  how extension methods are declared, simply adding a "this" word turns your method into an Extension Method.

Posted on Monday, January 28, 2008 1:38 AM | Back to top

Comments on this post: LINQ - "Where" Extension Method

# re: LINQ - "Where" Extension Method
Requesting Gravatar...
I think there is a typo in this post:
".Where(participant=> participants.Score > 80)"
should be participant in both cases.
Left by Sam Piper on Feb 23, 2008 9:48 AM

# re: LINQ - "Where" Extension Method
Requesting Gravatar...
Very useful !! Thanks :)
Left by tom-i on Jun 03, 2008 3:56 AM

# re: LINQ - "Where" Extension Method
Requesting Gravatar...
great, it's easy and useful! :)
Left by c# on Jul 29, 2008 4:58 AM

# re: LINQ - "Where" Extension Method
Requesting Gravatar...
Is there a way we can build our where condition dynamically and ammend later lke,

from u in db.tblUser
where u.FirstName.StartsWith(txtFirstName)

if (txtLastName.Text != String.Empty)
where u.LastName.StartsWith(txtLastName)

where u.Sex == Convert.ToInt16(cboSex.SelectedValue)

How can I add these 3 where conditions to my original query.
Left by nalaka on Jul 29, 2008 5:04 PM

# re: LINQ - "Where" Extension Method
Requesting Gravatar...
simple and usefull. Great :)
Left by Guf on Sep 04, 2008 1:08 AM

Your comment:
 (will show your gravatar)

Copyright © Shahed Khan | Powered by: | Join free