👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Using LINQ First to Select Single Data

Using LINQ First to Select Single Data

LINQ

26 Articles

Improve

In this article, let's learn about how to use First and FirstOrDefault in LINQ in .NET.

Note: If you have not done so already, I recommend you read the article on Using LINQ Where to Filter Data in Collections.

Table of Contents

  1. Introduction
  2. Search forward for an element using First
  3. Search forward for an element using FirstOrDefault
  4. When to use which?
  5. Summary

Introduction

We can select single piece of data from a collection using LINQ. We're going to use First() and FirstOrDefault() methods. Let's take a look at each of these methods and how they work.

Search forward for an element using First

When searching, some of the methods will actually throw an exception if you don't find anything based on the expression. So, for example, first, you pass in your lambda expression of what you're looking for. It searches forward in the collection and finds that first one that matches the expression and returns that. If it doesn't find it, it throws an exception.

Code Sample - LINQ First

Demo - LINQ First Clause Demo

Let's try LINQ First

  • We have Product class with following properties - Id, Name, Color, Price, Size
  • Enter the column names you would like to select.
  • Click on First Button
  • Click on reset to try other combination
Id Name Color Price Size
1 Shirt Black 1000 18
2 Shirt Red 1500 28
3 Shirt Black 2000 38
4 Shirt Red 2500 48
5 Shirt Brown 3000 58
6 Shirt White 3500 68

Search forward for an element using FirstOrDefault

The FirstOrDefault(), you pass in a lambda expression of what you're looking for and optionally a default value. What it does is it searches forward in the collection, and it returns null if no value is found, or if you supply a default value, it will return that value instead of null.

Code Sample - LINQ FirstOrDefault

Demo - LINQ FirstOrDefault Clause Demo

Let's try LINQ FirstOrDefault

  • We have Product class with following properties - Id, Name, Color, Price, Size
  • Enter the column names you would like to select.
  • Click First Or Default Button
  • Click on reset to try other combination
Id Name Color Price Size
1 Shirt Black 1000 18
2 Shirt Red 1500 28
3 Shirt Black 2000 38
4 Shirt Red 2500 48
5 Shirt Brown 3000 58
6 Shirt White 3500 68

When to use which?

So now I've shown you all these LINQ queries, but which ones do you use and when do you use them? So I'm going to do a comparison between the First() versus the FirstOrDefault() methods. If you expect the element to be present in the collection, you can use First(). But if you're not sure if that element is present, then use FirstOrDefault(), and that has to do with exceptions, right? Because the First() is going to throw an exception if something is not found. So if you want to handle that or maybe rethrow an exception if something's not found, you could use First(). Me, I'd rather not deal with exceptions, so I really prefer FirstOrDefault(). Also, this gives us back a null or some other default value, and I find it easier to check for null than actually check for an exception.

Summary

In this article we learn't how to locate single data within collection using First and FirstOrDefault. We saw that there's an option to supply your own default value, that sometimes you need to catch exceptions or sometimes you need to check for null or that default value, depending on the method that you're calling. Now I always like using all the OrDefault() methods so that I can avoid anybody throwing exceptions. To me, throwing exceptions are not something that you want to use as a regular way of programming. Those should be exceptions. All these can be used with any IEnumerable or IQueryable types.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • LINQ
  • First
  • FirstOrDefault
  • Search forward