2

Is there any difference between

 DataFeedManager.LoadAllDataFeeds().FirstOrDefault(d => d.ItemID == itemId);

and

DataFeedManager.LoadAllDataFeeds().Where(d=>d.ItemID = itemId).FirstOrDefault();

Is there a particular reason to prefer one over the other?

Allen Wang
  • 978
  • 8
  • 12

1 Answers1

6

Prefer the first for brevity if you're comfortable with it.

Prefer the second for clarity if you're not as familiar with the various overloads available.

(Where "you" is really "everyone working on the code" of course.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    apart from coding habits, what else? – Allen Wang Apr 03 '12 at 17:46
  • Speaking of performance: There might be a tiny overhead with the second form because of the extra method call, but that is most probably negligible. The "it has to load the list twice" argument needs verification, as the .Where() is most probably lazy. You could also check what happens with CPU cache, but this will need attention from a specialist. – PPC Oct 09 '12 at 21:28
  • 1
    @PPC: What "it has to load the list twice" argument? `Where` is certainly lazy in LINQ to Objects. I'm not sure who's "speaking of performance" in the first place... – Jon Skeet Oct 09 '12 at 21:35
  • Beware non-production or less-than-complete implementations of LINQ providers. SQLite-net for example will realize the entire table into memory when doing Table.FirstOrDefault(f) and will run a sql statement when doing Table.Where(f).FirstOrDefault(). On a Corei7 with 30,000 records, the difference is 15ms to 2200ms. – Shahar Prish Apr 20 '14 at 21:14
  • I prefer the first variant too. – mihkov Sep 21 '17 at 13:54