Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, In code below I have to call same expression twice to get First Item that matches STRING. Is there any better way of doing this?

if (InvoiceDetails.Where(x => x.IMEINumber == boostReconcile.BoostSheetLine.BoostImei).Count() > 0)
                   {
                       boostReconcile.InvoiceDetail = InvoiceDetails.Where(x => x.IMEINumber == boostReconcile.BoostSheetLine.BoostImei).First();
                   }


What I have tried:

There is nothing to describe about the code.
Posted
Comments
Philippe Mori 12-Feb-16 22:46pm    
Maybe, if you would look function that IntelliSense show in the dropdown you would have been able to figure out yourself the most appropriate function to use.

If it's ok to set boostReconcile.InvoiceDetail to its default value (null if it's a reference type) when there is no InvoiceDetail with a matching IMEINumber then you can do this instead:
C#
boostReconcile.InvoiceDetail = InvoiceDetails.FirstOrDefault(x => x.IMEINumber == boostReconcile.BoostSheetLine.BoostImei);


If there should ever be only 0 or 1 InvoiceDetail that matches this criterion, you should use SingleOrDefault() instead:
C#
boostReconcile.InvoiceDetail = InvoiceDetails.SingleOrDefault(x => x.IMEINumber == boostReconcile.BoostSheetLine.BoostImei);
 
Share this answer
 
v2
Try:
C#
boostReconcile.InvoiceDetail = InvoiceDetails.FirstOrDefault(x => x.IMEINumber == boostReconcile.BoostSheetLine.BoostImei);

Enumerable.FirstOrDefault(TSource) Method (IEnumerable(TSource), Func(TSource, Boolean)) (System.Linq)[^]

NB: That will overwrite the property with null if there are no matching records. If you already have a value in the property, and you don't want to overwrite it, then try:
C#
boostReconcile.InvoiceDetail = InvoiceDetails.FirstOrDefault(x => x.IMEINumber == boostReconcile.BoostSheetLine.BoostImei) ?? boostReconcile.InvoiceDetail;
 
Share this answer
 
v2
Comments
Sascha Lefèvre 9-Feb-16 12:43pm    
+5
Philippe Mori 12-Feb-16 22:40pm    
Well, I would not recommend last code fragment. It is weird to assign the value of a variable to that variable.
Try
C#
var g = InvoiceDetails.Where(x => x.IMEINumber == boostReconcile.BoostSheetLine.BoostImei);
if (g.Count() > 0)
                   {
                       boostReconcile.InvoiceDetail = g.First();
                   }
 
Share this answer
 
Comments
Sascha Lefèvre 9-Feb-16 12:43pm    
No need to Count() all if you only need the first - Any() would be better.
Richard Deeming 9-Feb-16 12:47pm    
Stop reading my mind! :)
Richard Deeming 9-Feb-16 12:46pm    
That will still iterate the list / execute the query twice.

Also, it's generally better to use .Any() rather than .Count() > 0 - the only time .Count() will perform better is if the source is an ICollection<T>, which isn't the case here.
Philippe Mori 12-Feb-16 22:42pm    
Almost no improvement to the original code. Only marginally better. See other solutions which are far better.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900