Click here to Skip to main content
Click here to Skip to main content

LINQ – Element Operations

, 12 Jan 2014
Rate this:
Please Sign up or sign in to vote.
CodeProject In the last blog post, we have discussed about Click Event and Change Event in jQuery. You can read that article here. In this article we will go over Element Operations in LINQ. Different Element Operations in LINQ are following: ElementAt/ElementAtOrDefault - Returns the element at a s

LINQ

In the last blog post, we have discussed about Click Event and Change Event in jQuery. You can read that article here. In this article we will go over Element Operations in LINQ.

Different Element Operations in LINQ are following:

  • ElementAt/ElementAtOrDefault - Returns the element at a specified index.
    • Look at the example code below. We have a sequence of 2 strings, Hello and World. If we write notEmpty.ElementAt(1), we will get World as output. So this is zero based indexing and World is the element at the position 1.
  • First/FirstOrDefault - Returns the first element of a collection.
    • You may use this Element Operation quite frequently with queries, particularly when work with databases. So using this operation, instead of getting back an IEnumerable collection, we will just get back one element reference.
  • Last/LastOrDefaultReturns the last element of a collection.
  • Single/SingleOrDefault - Returns a single element.
    • This Element Operator returns a single element instead of getting IEnumerable collection. So it is almost similar to First/FirstOrDefault operator. The primary difference is that, with Single, if there isn’t just one result in the sequence, it will throw an exception.

Example:

sting[] empty={ };

sting[] notEmpty={ “Hello”,“World”};

var result=empty.FirstOrDefault(); //null

result=notEmpty.Last();                    //World

result=notEmpty.ElementAt(1);      //World

result=empty.First();                          //InvalidOperationException

result=notEmpty.Single();                 //InvalidOperationException

result=notEmpty.First(s=>s.StartsWith(“W”));

Reference: Arun Ramachandran (http://BestTEchnologyBlog.Com)


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Arun Ramachandran India
Software Developer
India India
Arun Ramachandran is a Software Engineer having hands on experience in different Microsoft Technologies who is presently working in Experion Technologies, India. He has written over 95 articles on the subject on his blog at http://BestTEchnologyBlog.com. Along with 3 years of hands on experience he holds a Master of Computer Applications degree from Cochin University of Science & Technology (CUSAT).

Comments and Discussions

 
QuestionIt's a nice roundup of a couple of extensions Pinmemberlespauled13-Jan-14 6:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 13 Jan 2014
Article Copyright 2014 by Arun Ramachandran India
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid