LINQ ANY () with First () and FirstOrDefault ()

I wrote such code

TotalCashIn = totals != null && totals.Any() ? totals.First().TotalCashIn : null; 

And I was accused of this code and told to write like this:

 TotalCashIn = totals != null ? totals.FirstOrDefault().TotalCashIn : null; 

But I am wondering if I am not getting an exception if totalals count is 0? Should I also check this with .Any() or .Count() ?

+7
performance c # linq
source share
3 answers

You can use the null-condition operator to make it all a lot easier, assuming the totals element type is a reference type:

 TotalCashIn = totals?.FirstOrDefault()?.TotalCashIn; 

Wherein:

  • If totals is null, the total result is zero due to the first null-conditonal operator
  • If totals empty, FirstOrDefault() returns null, so the total result is null due to the second statement with a null condition
  • Otherwise, the result is the TotalCashIn property of the first element
+13
source share

It depends on totals IEnumerable or IQueryable . If it is IQueryable , you can also use the following:

 TotalCashIn = totals?.Select(t => (int?)t.TotalCashIn).FirstOrDefault(); 

It would be better if you select only the required field, and not all the columns in the table.

0
source share

If the resulting values ​​are an object of a reference type, then, indeed, FirstOrDefault can return null, and therefore you cannot call TotalCashIn.

If, on the other hand, the totals are a value type, then FirstOrDefault will return the actual value, and you can call TotalCashIn. For example, if the totals are a sequence of integers, then FirstOrDefault will return zero (not null!).

If TotalCashIn is an int extension function, then if it would be ideal to call TotalCashIn after FirstOrDefault:

 IEnumerable<int> totals = ... int firstInt = totals.FirstOrDefault(); var totalCashIn = firstInt.TotalCashIn(); // extension function of int 

or in one statement:

 var totalCashIn = totals.FirstOrDefault().TotalCashIn(); 

Whether it is possible to use an operator with a null condition (?.) Depends on the definition of your TotalCashIn, especially if your collection does not have an element at all.

If you say: "TotalCashIn represents the amount of money that I cashed while ...", then TotalCashIn can have a zero value, but not a zero value.

If you want to get a null value in the case of empty collections, consider using Select to select the property you want before your FirstOrDefault:

 var totalCashIn = totals.Select(item => item.TotalCashIn).FirstOrDefault(); 

If the TotalCashIn property is a value type, then this will return zero if the totals collection is empty

As for your question, should I use Count () or Any (): never use Count () to check if your sequence has any elements if you are not sure if your input sequence is an ICollection. In fact, it is a waste of computing power to access all the elements of your sequence, if after the first element you already know that there are elements.

0
source share

All Articles