0

There is something I don't really find an answer to:

db.Sensors.Where(s => s.Id == id).Single();

Is the Single() here necessary? There will normally only be given a single result, but is it really necessary to add it or is it redundant? What is the best practice?

Marijke
  • 153
  • 3
  • 14
  • 1
    `Single()` used to return single object from `Sensor`, and `Where` returns `IEnumerable`. – Tetsuya Yamamoto Aug 07 '18 at 08:38
  • That depends on your use-case. However `Where` always returns an `IEnumerable`, whilst `Single` returns an instance. We can´t know what you need. – MakePeaceGreatAgain Aug 07 '18 at 08:38
  • Where might return nothing or array of objects. Single ensures that your query returns one and only one result. – Renatas M. Aug 07 '18 at 08:38
  • 3
    You could just do `.Single(s => s.Id == id);` to get the same result. – default Aug 07 '18 at 08:40
  • Ok thanks for all the feedback everyone, I guess I better use Single because there is no use in getting a list if I only want one object. – Marijke Aug 07 '18 at 08:43
  • 1
    also, see this thread for reference: https://stackoverflow.com/questions/2724096/linq-single-vs-first – default Aug 07 '18 at 08:49
  • @Marijke - Note the performance implication mentioned in my answer. When dealing with sensors sometimes speed is critical – Milney Aug 07 '18 at 08:49
  • Look at SingleOrDefault(),(check for default rather than throw error(s) First(), and FirstOrDefault() instead perhaps. – Mark Schultheiss Aug 05 '20 at 19:19

5 Answers5

4

You can combine them:

db.Sensors.Single(s => s.Id == id);
Mel Gerats
  • 2,234
  • 1
  • 16
  • 33
2

It may not be necessary in your specific situation, but Single() enforces that there can only be one result and will throw an exception if there is more or less than one. In some situations where more or less than one result would highlight a bug then this can be useful.

From MSDN:

Single(IEnumerable)

Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

Single(IEnumerable, Func)

Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.

JasperMoneyshot
  • 357
  • 3
  • 15
1

Where returns an IEnumerable where as Single returns one object or throws an exception if there is 0 or more than one object, as other answers allude -

HOWEVER NOTE: This means it has to check ALL the objects, and therefore has a performance penalty - If you are confident there will only be one (i.e. there is a unique constraint on a database) then you can use First() instead, to get a single object and not bother checking all the others for duplicates.

Milney
  • 6,253
  • 2
  • 19
  • 33
1

Use Single() when you feel it should explicitly always return 1 record. This will help you avoid logical errors.

If you are sure, db.Sensors.Where(s => s.Id == id) is going to return one and only one record, then Single() is not required.

Single() will throw an exception if db.Sensors.Where(s => s.Id == id) return null or more than one row. Single() will help you to know it is returning more than one row or null by throwing an exception.

Trupti J
  • 512
  • 1
  • 4
  • 16
-1

Yes. There is a light different.

The method Where returns an IEnumerable, means a list which contains zero or only objects which match your ID

The method Single returns a single object. But it throws an exception if there isn't an object or more than one object which matches your ID.

By your requirement, if you need a list to loop, use Where, if need to get an object, use Single

You chose your method

Antoine V
  • 6,998
  • 2
  • 11
  • 34