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 269209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:48:02+00:00 2026-05-11T23:48:02+00:00

I am working on a database access layer project and decided to use Linq

  • 0

I am working on a database access layer project and decided to use Linq to SQL for it. One of the things I wanted to do was provide an API that can take Linq Expressions as arguments to retrieve data. For instance, I wrote this API now that looks like this:

 public Result GetResults(System.Linq.Expressions.Expression<Func<Result, bool>> predicate)
    {
        Result result = db.Results.SingleOrDefault(predicate);
        return result;
    }

You can then use this API to query the database for a Result row that satisfies certain conditions, for example:

Result result = Provider.GetResults(r => r.ID == 11);

This works very well. I am able to get the one row I want based on my conditions.

Next step was taking this to be able to get multiple objects back from the database.

The way I got it to work was like this:

public List<Result> GetResults(System.Linq.Expressions.Expression<Func<Result, bool>> predicate)
    {
        List<Result> results = db.Results.Select(r => r).Where(r => r.ID == 11).ToList<Result>();
        return results;
    }

As you can see, I call a Select with r => r, this gives me back everything and then I use a Where to filter to what I need.

It works… but something tells me that I am doing it really ugly. I could be wrong, but doesn’t this pull EVERYTHING out of the Results table then filters it? or does it put together the correct SQL statement that does filter at the database level?

Anyway… I would highly appreciate some guidance on how I can accomplish this task. How do I write an API that takes a Linq Expression as an argument and returns a set of objects from the database based on that expression.

Thanks!

  • 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-05-11T23:48:02+00:00Added an answer on May 11, 2026 at 11:48 pm

    The Select(r=>r) does nothing (except change from Table<T> to IQueryable<T> – but nothing useful). And I assume you intended to pass predicate to the Where?

    Indeed, this doesn’t pull everything out and filter it – the appropriate WHERE (TSQL) clause is generated. This is possible because of “deferred execution” and “composability” – meaning: it doesn’t actually execute anything until you start iterating the data (in the ToList()) – until then you are simply shaping the query.

    You can see this by doing somthing like:

    db.Log = Console.Out;
    

    and look at the TSQL. Or run a TSQL trace. To make it prettier, simplify it to:

    return db.Results.Where(predicate).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 145k
  • Answers 145k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I found the solution The part: var elementHolder = element.holder;… May 12, 2026 at 8:53 am
  • Editorial Team
    Editorial Team added an answer So you want to put variables definitions in header file?… May 12, 2026 at 8:53 am
  • Editorial Team
    Editorial Team added an answer Alas, the answer was right in the views help: Column… May 12, 2026 at 8:53 am

Related Questions

A project I'm working on at the moment involves refactoring a C# Com Object
Am working on a business layer for a complex web application and am temporary
I am working on an application that uses Oracle's built in authentication mechanisms to
I joined a new company about a month ago. The company is rather small

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.