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

  • SEARCH
  • Home
  • 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 8463893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:34:37+00:00 2026-06-10T14:34:37+00:00

What should be the right practice to handle duplicate linq queries to Database? By

  • 0

What should be the right practice to handle duplicate linq queries to Database?
By duplicate queries, I mean such queries that recurs in my code.

Example:

I notice that in my code recurs following query:

dataContext.Users.Where(u =>u.IsActive && u.IsAdmin).OrderByDescent(u => u.Name)

Where should I place the method that contains above query. Should it be UserRepository(in this manner, I will probably end with god class which is bad practice)? or should I create class architecture to handle this.

I wonder what are your thoughts about this. And what class structure in your opinion will be most generic and vulnerable to reuse.

I have in mind that, I can use a store procedure as a place for recurring queries, but I don’t want to go that way.

  • 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-10T14:34:38+00:00Added an answer on June 10, 2026 at 2:34 pm

    Yes, it can be in the UserRepository, but you can also build up an Godclass for standardized queries (Create, Delete, and so on) and take use of an Service Layer or Extensions to Filter the results from GetAll() Method in the UserRepository.

    Update
    I’m trying to show it a little bit more detailed.

    1. Build an generic repository that holds all your statements to retrieve/store your data.
    2. Create an service layer that consume from/send to the repository.
    3. Add extension methods to filter for specific results. (featured with c# 3.0)

    Some code:

    // Repository (non-generic)
    private IQueryable<User> GetUser()
    {
      var results = from u in _db.Users
                    select new User
                    {
                      ID = u.ID,
                      Username = u.Username,
                      // and so on
                    };
    
      return results;
    }
    
    
    //Service Layer  
    private User GetUser(string Username)
    {
       User u = _repository.GetUser().WithUsername(username)
                                     .SingleOrDefault();
    } 
    
    // Extension Method
    public static IQueryable<User> WithUsername(this IQueryable<User> qry, string username)
    {
      return from u in qry
             where u.Username == username
             select u;
    }
    

    This approach is related to Rob Conerys MVC StoreFront Preview. The implementation maybe vary in other projects, but this is one way to do it.

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

Sidebar

Related Questions

Is using view in db design right method or we should handle it code
Is it a good practice to comment code that is removed? For example: //
Seems like there should be... Right now it just seems like magic that you
I've read that best practice dictates fats models, skinny controllers. Models should contain business
I have an image icon in an HTML page that should show right to
This should work right? I have not a clue as to why it's not.
The menu is what I want, when mouse over the left, the right should
should be simple right? but have not found any anwsers :
Constant height so just left/right images should be necessary instead of having 4+, right?
What is the value of document.write(false == null) . It should be true right

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.