Subscribe to Subscribers
Talk to Webmaster Tony John


Resources » Code Snippets » LINQ Samples

How to write Fluent Linq Queries?


Posted Date:     Category: LINQ Samples    
Author: Member Level: Gold    Points: 25


This article helps you to understand Linq a little more easily. I have used Fluent expressions to write the LINQ queries.I have also used normal queries to that you can differentiate between the fluent queries and the normal queries.Its easy to write both types of queries, you have to decide in which way you want to write that.



 


Example 1:
We have an array of strings named 'names'.
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };


Now we are using the IEnumerable to specify the Datatype of the query that is string.We can use var also.Here we have used the Where clause,OrderBy clause and the select clause,the order is not important between Where and OrderBy clauses, but select must be the last Clause.Since where clause filters the data so it is always recommended that to use where clause before any clauses so that your query will be a little faster if you have many values in your query.Like when you are doing a cross join you want your query to run faster, at that time you can use this type of expressions.

IEnumerable query = names
.Where (n => n.Contains ("a"))
.OrderBy (n => n.Length)
.Select (n => n.ToUpper());

foreach(var i in query)
{
Console.WriteLine(i);
}


Here at first i am filtering my list and then ordering it by its length i.e. smaller length word will be at first and then the bigger word length.Afetr that the names are projected out as an upper case.
OR

var query = names
.Where (n => n.Contains ("a"))
.OrderBy (n => n.Length)
.Select (n => n.ToUpper());

foreach(var i in query)
{
Console.WriteLine(i);
}


Same as that of previous one but using the keyword var.

// The same query constructed progressively:

This is same as that of previous one but we are doing all the things progressively.At first we are filtering it then ordering it and then selecting out using the select clause.
IEnumerable filtered   = names.Where(n => n.Contains ("a"));
foreach(var i in filtered)
{
Console.WriteLine(i);
}
IEnumerable sorted = filtered.OrderBy (n => n.Length);
foreach(var i in sorted)
{
Console.WriteLine(i);
}
IEnumerable finalQuery = sorted.Select (n => n.ToUpper());
foreach(var i in finalQuery)
{
Console.WriteLine(i);
}


Example 2:

In this example we are selecting out only those names whose length is the smallest.it can be done in four ways given below.
string[] names = { "Tom", "Dick", "Harry", "Mary", "Jay" };

names.Where (n => n.Length == names.OrderBy (n2 => n2.Length)
.Select (n2 => n2.Length).First())
foreach(var i in names)
{
Console.WriteLine(i);
}

var query =
from n in names
where n.Length == (from n2 in names orderby n2.Length select n2.Length).First()
select n;

query.Dump ("Same thing as a query expression");

query =
from n in names
where n.Length == names.OrderBy (n2 => n2.Length).First().Length
select n;

query.Dump ("Reformulated");

query =
from n in names
where n.Length == names.Min (n2 => n2.Length)
select n;

query.Dump ("Same result, using Min aggregation");



If you have any doubts feel free to ask.





Did you like this resource? Share it with your friends and show your love!


Responses to "How to write Fluent Linq Queries?"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Different Sorting techniques (delegate with anonymous method, lamda)
    Previous Resource: Filtering data table using LINQ query
    Return to Resources
    Post New Resource
    Category: LINQ Samples


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.