394

I have a question about LINQ query. Normally a query returns a IEnumerable<T> type. If the return is empty, not sure if it is null or not. I am not sure if the following ToList() will throw an exception or just a empty List<string> if nothing found in IEnumerable result?

   List<string> list = {"a"};
   // is the result null or something else?
   IEnumerable<string> ilist = from x in list where x == "ABC" select x;
   // Or directly to a list, exception thrown?
   List<string> list1 = (from x in list where x == "ABC" select x).ToList();

I know it is a very simple question, but I don't have VS available for the time being.

RAM
  • 2,257
  • 2
  • 19
  • 41
David.Chu.ca
  • 37,408
  • 63
  • 148
  • 190

7 Answers7

650

It will return an empty enumerable. It won't be null. You can sleep sound :)

Pang
  • 9,564
  • 146
  • 81
  • 122
leppie
  • 115,091
  • 17
  • 196
  • 297
50

You can also check the .Any() method:

if (!YourResult.Any())

Just a note that .Any will still retrieve the records from the database; doing a .FirstOrDefault()/.Where() will be just as much overhead but you would then be able to catch the object(s) returned from the query

kobaltz
  • 6,980
  • 1
  • 35
  • 52
Noich
  • 14,631
  • 15
  • 62
  • 90
  • 8
    Where does the question mention a database? – cja Nov 14 '13 at 10:00
  • 11
    You'll have to ask the one who edited, I didn't mention any DB :) – Noich Jul 06 '14 at 05:37
  • Point editor is making is sound, though, DB or not. I believe they are saying `.Any()` is just going to tell you if you have any matching records, at all, where doing an actual query to find a specific value might be null when `.Any()` is not. – vapcguy Dec 18 '14 at 03:45
  • 3
    The edit might actually be wrong. If using linq to entities, the db might shortcut this and no data at all is being sent to the client except a true or false – Mafii Aug 15 '16 at 13:28
24
var lst = new List<int>() { 1, 2, 3 };
var ans = lst.Where( i => i > 3 );

(ans == null).Dump();  // False
(ans.Count() == 0 ).Dump();  // True

(Dump is from LinqPad)

JP Alioto
  • 44,864
  • 6
  • 88
  • 112
19

.ToList returns an empty list. (same as new List<T>() );

Mark Cooper
  • 6,738
  • 5
  • 54
  • 92
Paul van Brenk
  • 7,450
  • 2
  • 33
  • 38
9

In Linq-to-SQL if you try to get the first element on a query with no results you will get sequence contains no elements error. I can assure you that the mentioned error is not equal to object reference not set to an instance of an object. in conclusion no, it won't return null since null can't say sequence contains no elements it will always say object reference not set to an instance of an object ;)

kay.one
  • 7,622
  • 6
  • 55
  • 74
8

It won't throw exception, you'll get an empty list.

Jimmy Chandra
  • 6,472
  • 4
  • 26
  • 38
  • What is the correct way to throw exceptions in case of getting null? Do we have to check it after the query with if-else statements? – SBU Sep 17 '21 at 08:04
  • @SBU you can check it with Any() and throw an exception yourself: var result = source.Where(s=>s.Name == "SBU"); if(!result.Any()) throw new Exception("Result Is empty"); – fs_dm Jan 27 '22 at 12:58
8

Other posts here have made it clear that the result is an "empty" IQueryable, which ToList() will correctly change to be an empty list etc.

Do be careful with some of the operators, as they will throw if you send them an empty enumerable. This can happen when you chain them together.

Mark Cooper
  • 6,738
  • 5
  • 54
  • 92
Spence
  • 28,526
  • 15
  • 68
  • 103
  • 3
    "Do be careful with some of the operators, as they will throw if you send them an empty enumerable. This can happen when you chain them together." -- This is what got me. I had a null returned value that I then fed into another query. This caused the second query to throw no matter what I cast it to because there was no value being fed into the second query. – trevorc Nov 30 '11 at 21:11