dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online MembersPhagu Mahato
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » LINQ Samples

Restriction and Projection Operators in LINQ


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


Want to learn about the LINQ Operators? This article would discuss about restriction and projection LINQ operators and how to use these LINQ operators in your program.



 


In this article we will learn how to use LINQ operators in our application. I would also be covering the entire range of Linq functionality in this sample code.

Before we proceed to the coding for using LINQ operators I would like to first briefly describe what is LINQ.

Defination of LINQ as given in books: Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# which have many operators. We will look through the restriction and Partition operators of LINQ.

Restriction Operator of LINQ



1. Where Clause

Suppose we have a list of strings and we want to filter it by the names which starts with 'A'.
string[] names = { "Asyl", "Bail", "Manu", "Agni", "Jay" };


In fluent syntax the query will be...
IEnumerable query = names.Where (name => name.StartsWith ("A"));
foreach(var item in query)
{
Console.WriteLine(item);
}
//Output
Asyl
Agni


In normal query syntax...
IEnumerable query = from n in names
where n.StartsWith ("y")
select n;
foreach(var item in query)
{
Console.WriteLine(item);
}
//Output
Asyl
Agni


Projection Operators of LINQ



1. Select Clause

Suppose we have a list of numbers and we want to select the numbers by adding 1 to it.
var numbers = { 6, 9, 4, 3, 0, 1 };
var query = from n in numbers select n+1;
foreach(var item in query)
{
Console.WriteLine(item);
}
//OutPut
7
10
5
4
1
2


2. Select Many Clause
Suppose we have two lists of numbers and we want to select the numbers from both the list where listA is less than listB.
var listA = { 6, 9, 4, 3, 0, 1 };
var listB = { 1, 2, 5, 8, 9, 5, 4};

var query = from a in listA
from b in listB
where a < b
select new {a,b};
foreach(var item in query)
{
Console.WriteLine(item.a + " < " + item.b);
}





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


Responses to "Restriction and Projection Operators in LINQ"

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: Filtering data table using LINQ query
    Previous Resource: Partitioning Operators in Linq
    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
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    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.