The LINQ query looks the same as any other query that filters based on name. We then
use the Singlemethod to let Entity Framework know that we expect a single result. If
the query returns no results, or more than one result, an exception will be thrown. If
there are potentially no matches, you can use the SingleOrDefault method, which will
return null if no results are found. Example 2-16 shows the FindGreatBarrierReef
method updated to account for the fact it may not exist in the database.
If two rows are found, Singleand SingleOrDefault will throw an error because there is not a
single result. If you just want the first result, and aren’t concerned if there is more than
one result, you can use Firstor FirstOrDefault.
Robert Dannelly blog, Over the internet you can find me by "robert dannelly blog". The blog is an ongoing blog of my software engineering over technologies C#, VB.NET, Java, React, JavaScript, .NET Core Current, SQL Server 7 - Current Version, GIT, GitHub, Jira, Azure, AWS and HTML5. “I have not failed. I've just found 10,000 ways that won't work.” Thomas A. Edison, please click on all links to help support this blog. Thank you. “As an Amazon Associate, I earn from qualifying purchases.”
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment