0

i want to know what is the defference between the following queries:

IQueryable<Sample> q1 = databaseContext.Samples
    .FirstOrDefault(x=>x.ItemID == itemID );

IQueryable<Sample> q2 = databaseContext.Samples
    .Where(x=>x.ItemID == itemID );

when sample has navigation property.

ASh
  • 34,632
  • 9
  • 60
  • 82
Qusay
  • 1
  • 2
  • The first one gives you a single item (or null) whereas the second one can give you any number of matching items. Perhaps you need to read the docs or run through some basic Linq tutorials. – DavidG Nov 16 '21 at 09:53

1 Answers1

0

Where returns an IEnumerable. You don't know how many items the predicate might match.

FirstOrDefault will get the first Sample that matches your predicate, or null if there are no items that match.

Ezra
  • 529
  • 4
  • 5