Using LINQ Single to Select Single Data

Using LINQ Single to Select Single Data

linq

14 Articles

Improve

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

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

Table of Contents

  1. Introduction
  2. Search for only one element using Single
  3. Search for only one element using SingleOrDefault
  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 Single() and SingleOrDefault() methods. Let's take a look at each of these methods and how they work.

Search for only one element using Single

If you're familiar with your collection and you know there's only a single item you're looking for, you can use the Single method, passing in your lambda expression of what you're looking for. It will search through the collection forward. It'll throw an exception if the collection is null or if more than one element is returned. So this one, it truly only wants a single value to match in that collection.

Code Sample - LINQ Single

Demo - LINQ Single Clause Demo

Let's try LINQ Single

  • We have Product class with following properties - Id, Name, Color, Price, Size
  • Enter the column names you would like to select.
  • Click on Get Single product
  • Click on reset to try other combination
Id Name Color Price Size
1 HL Road Frame Black 1000 58
2 HL Road Frame Red 1000 58
3 HL Road Frame Black 1000 58
4 HL Road Frame Red 1000 58
5 HL Road Frame Brown 1000 58
6 HL Road Frame White 1000 58

Search for only one element using SingleOrDefault

The other option is SingleOrDefault. Again, you pass in the expression and optionally a default value. Now this will throw an exception if the collection is null or more than one element is returned. Otherwise, if it doesn't find anything, it will return a null or whatever default value you supplied.

Code Sample - LINQ SingleOrDefault

Demo - LINQ SingleOrDefault Clause Demo

Let's try LINQ SingleOrDefault

  • We have Product class with following properties - Id, Name, Color, Price, Size
  • Enter the column names you would like to select.
  • Click on Single Or Default product
  • Click on reset to try other combination
Id Name Color Price Size
1 HL Road Frame Black 1000 58
2 HL Road Frame Red 1000 58
3 HL Road Frame Black 1000 58
4 HL Road Frame Red 1000 58
5 HL Road Frame Brown 1000 58
6 HL Road Frame White 1000 58

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() and Last() methods versus the FirstOrDefault() and the LastOrDefault() methods. If you expect the element to be present in the collection, you can use First() or Last(). But if you're not sure if that element is present, then use FirstOrDefault() or LastOrDefault(), and that has to do with exceptions, right? Because the First() and Last() 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() or Last(). Me, I'd rather not deal with exceptions, so I really prefer FirstOrDefault() or the LastOrDefault(). 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. Well, how about First() versus Single()? Well, these are pretty similar because if you expect the element to be present, you can use First() or you can use Single(). If you want to handle or throw an exception if it's not found, you could use First(), you could use Single(). Here's where it really matters. First() is only going to search until it finds the element, then it stops. Single(), however, has to search the entire list every time. And the reason why is because, remember, it has to check to see if there are multiples so it knows whether or not to throw that exception. So what does that mean? It means that First() is faster than Single(). Single() is always going to be slower because it always has to search. If you've got a collection of 10,000 items, and First(), you're looking for the first thing, and it happens to get it at five, it's done. Single() still has to go through all the rest of the collection. So what about SingleOrDefault() versus FirstOrDefault()? Well, if you expect the element to be present, SingleOrDefault() works. If you're not sure if the element is present, use FirstOrDefault(). If you want to handle or throw an exception if something's not found, SingleOrDefault()'s fine. But if you don't want to handle exceptions, you're really more interested in getting back a null or your own default value, take advantage of FirstOrDefault(). Now again, SingleOrDefault() must search the entire list every time you call it, whereas FirstOrDefault() searches only until it finds the element. That means that SingleOrDefault() is always going to be slower than FirstOrDefault(). So again, I would typically use the FirstOrDefault().

Summary

In this article we learn't how to locate single data within collection using Single and SingleOrDefault. 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.

  • Linq
  • Single
  • SingleOrDefault
  • Full Search