How to handle case of matches in List.First in C #?

In IEnumerable.First Function , how do I handle the case if there are no matches? Currently, he is just falling ...

MySPListItem firstItem = itemCollection.First(item => !item.isFolder); if (firstItem != null) { TreeNode firstNode = GetNodeByListItem(my_treeview.Nodes, firstItem, ReportObject); if (firstNode != null) { ReportObject.log("Selecting the first PDF"); selectPDF(my_treeview, firstNode, queryStr_param); } } 

Error message:

The sequence does not contain the corresponding element.
Stacktrace: in System.Linq.Enumerable.First [TSource] (IEnumerable 1 source, Func 2 predicate)

+7
c # linq
source share
6 answers

Well, if he is exceptional , so that he is not the first,

 try { var first = enumerable.First(); } catch (InvalidOperationException) { // Oops, that was exceptional. } 

If you expect that in some acceptable situations there may not be the first,

 var first = enumerable.FirstOrDefault(); if (first == default(someType)) // null for reference types. { // Ok, I need to deal with that. } 
+14
source share

While you do a null check in a search, you are not using your predicate. Line

  foundItem = itemCollection.Find(item => item.item.ID == PDFID); 

You can exclude it item null (have you added the null element to the collection?) Or item.item is null (are you sure it is always there?).

You can do:

 foundItem = itemCollection.Find(item => item != null && item.item != null && item.item.ID == PDFID); 

More chat, but you will not get a NullReferenceException .

Edit Ok, you changed your question. Now you do First . The First method will throw an exception if nothing is found. Instead, use FirstOrDefault , which will return null for the class or default value for the struct .

  foundItem = itemCollection.FirstOrDefault(item => item != null && item.item != null && item.item.ID == PDFID); 
+7
source share

Replace FirstOrDefault first:

 MySPListItem firstItem = itemCollection.FirstOrDefault(item => !item.isFolder); 
+3
source share

Quote from the MSDN site you contacted:

The first element that matches the conditions defined by the specified predicate, if found; otherwise, the default value for type T.

The return value is described here. Therefore, if no match is found, the default value is returned for type T , which means null for reference types, and things like 0 , false and co. for value types.

So, in your code code, just check this default value and you are fine :-). What you cannot do is simply use the return value, as this, for example, can lead to a NullReferenceException if you use a reference type.

+2
source share

Check if the result matches null :

  if (result == null) { Console.WriteLine("Not found"); } 

There is a clear example demonstrating what to do if an item is found / not found here.

0
source share

It should not throw an exception, you need to handle the case when it returns the default value (T). Perhaps you get a NullReferenceException because you are not handling the case when it returns null. For example:

 IEnumerable<Cars> cars = GetCars(); Car car = cars.Find(c => c.Model == "Astra"); if(car != null) { // You found a car! } 

If you did the same for the structure, you should check it by default instead. For example, Ints will be 0:

 int[] ints = new int[] { 1, 4, 7 }; int myInt = ints.Find(i => i > 5); if(myInt != 0) // 0 is default(int) { // You found a number } 
0
source share

All Articles