Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8901071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:15:29+00:00 2026-06-15T01:15:29+00:00

We have a repository (Entity Framework) which queries for single records – only a

  • 0

We have a repository (Entity Framework) which queries for single records – only a single record should exist for any given query. Initially, our queries were SingleOrDefault().

Clearly, multiple results in the query will throw exception; and none are wrapped by try/catch. Rather than wrapping these queries in a try/catch block, I proposed an extension method as follows:

  public static bool IsEmpty<T>(this IQueryable<T> Query, out int Count) {
     Count = Query.Count();
     return Count == 0;
  }

This has advantages in more ways than simply determining if I have an empty query return or single result return.

The alternative is to wrap my query in a try/catch. My question is whether the extension method or expense of catching an exception is preferred. So as not be subjective, I am specifically referring to the cost of catching and throwing an exception versus the cost of the Count() method.

Although the database is expected to only return a single record, my approach is that the database will contain unexpected records. I don’t perceive this to be an exceptional event, therefore I do not perceive the need for throwing an exception.

The typical implementation of the extension method is as follows:

     var query = Repository.All().Where(*/ some criteria */);

     int count;
     if (query.IsEmpty(out count)) {
        // handle empty return
     } else if (count > 1) {
        // handle unexpected returns
     }

     return query.Single();

Edit
An important note: we want to be informed of ambiguous results and how many records are returned.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:15:30+00:00Added an answer on June 15, 2026 at 1:15 am

    Be careful not to execute the query multiple times by accident.

    var query =
        Repository.All()
        .Where(*/ some criteria */)
        .Take(2) //magic here
        .ToList();
    
    if (query.Count == 0) {
        // handle empty return
    } else if (query.Count > 1) {
        // handle unexpected returns
    }
    
    return query.Single();
    

    Query the TOP 2 rows to handle all cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Entity Framework Code First project I have a repository object which contains
I am mocking a repository that should have 1 entity in it for the
I have implemented Generic repository which only depends on IUnitOfWork (in Infrastructure.Repositories Library) and
I have an ASP.NET MVC application which uses Entity Framework to get data. I
I have implemented a simple repository pattern for the Entity Framework in a web
I have this ASP.NET app, in which I use Entity Framework to connect to
Assume that I have the following little console application which uses Entity Framework 5:
I have repository on GitHub to which I commit regularly from my local computer.
I'm using Visual SVN on my Windows Box. I have Repository Application , which
I have a repository where I'm trying to set up result caching. I've only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.