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 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

Related Questions

I am working on a new application that uses a jet (MS Access) database.
I'm looking at using LINQ to SQL for a new project I'm working on,
I'm working on a web application project using ASP.NET MVC3 and database in SQL
I am working on a .NET web application that uses an SQL Server database
I'm currently working on a project that uses data access on different databases. Our
I am currently working on a private project that is going to use Google's
A project I'm working on contains an MDB (acecss database) file. I'd like to
I have a database working in my local sql server 2005 express edition. I
I'm working on database migration program and I'm trying to migrate one database to
Am working on a .net 2.0 windows application. The data access layer[DAL] executes stored

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.