Clear ;. First is: 0. First is : 0. First is: 8. First is : 8. WriteLine first. Seats ;. Red, 2. Red , 2. WriteLine firstOrDefaultCar. WriteLine "There are no cars on the list. There are no cars on the list. Single , I have used. Single instead of. First since excepting if the non-empty result set has more than one match makes sense here: it is a lookup by what we presume is an exact match of unique primary key otherwise as JacquesB points out, since the query isn't ordered, we don't really know what we're keeping and what we're throwing away with.
But first a clarification:. First can be written simpler as. FirstOrDefault which does the same - returns the first item, or null if the result is empty. But you probably have a bug here: First indicates you might have multiple items and want to select the first item - but since there is no ordering involved it means you will get a random item. This is probably not what you want! If you really expect there could be multiple matches, then just return the list of matches.
If you expect a single item, use Single instead, and if you expect either a single item or nothing, use SingleOrDefult which will also return null if there was no match.
Now to the interesting question: What to return from a method which finds either a single object or nothing? Return null if the item is not found. Advantages: this is really simple. Disadvantages: The client have to check for null which is easily forgotten becaus the type system does not enforce this check. This might lead to hard-to-debug NullReferenceExceptions down the line. Not recommended. Throw an exception if the object is not found. Advantages: Simple and safe.
Disadvantages: Should only be used if it indicates a bug if the object does not exist. Create a separate method which allow you to check if the object exists eg. This method returns a boolean. Advantages: Clear code on the client side. Disadvantages: Will hit the database twice unless you use some kind of caching, so it may be expensive.
Also more code. Use the TryXXX pattern. Returns a boolean which indicates if the object exists and places the object if present in an out parameter. Advantages: This pattern is already known from say Dictionary. TryGetValue so readers will not be confused. Disadvantage: The out parameter leads to somewhat ugly code, although this is improved in C 6. This requires the client to explicitly check and unwrap the object.
Advantages: Much safer and more elegant than using null. Give me the first item matching this criteria.. Your code will throw an exception if it tries to use it. An alternative if you don't like testing for nulls, or empty objects, though is to always return a list of objects. Sign up to join this community. The best answers are voted up and rise to the top. Sometimes the value of default TSource is not the default value that you want to use if the collection contains no elements.
The following code example uses both techniques to obtain a default value of 1 if a collection of numeric months is empty. Because the default value for an integer is 0, which does not correspond to any month, the default value must be specified as 1 instead.
The first result variable is checked for the unwanted default value after the query has finished executing. The FirstOrDefault method does not provide a way to specify a default value. In the second call to the method, there is no element in the array that satisfies the condition.
Skip to main content. This browser is no longer supported. Download Microsoft Edge More info.
0コメント