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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:31:12+00:00 2026-05-11T01:31:12+00:00

Is it possible to pass parts of a linq Query into a function? I

  • 0

Is it possible to pass parts of a linq Query into a function? I want create a common interface for my DAL that always uses the same query interface. For example,

List<T> Get(Join j, Where w, Select s){         return currentDataContext<T>.Join(j).Where(w).Select(s).ToList();     } 

Is this sort of thing possible? I’m thinking it would be done with expression trees, but I haven’t been able to find examples of it.

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T01:31:13+00:00Added an answer on May 11, 2026 at 1:31 am

    Well, the ‘join’ is tricky, because it is very hard to express a join – but things like where / select / orderby are pretty easy…

    Really, it is just a case of combining the various LINQ methods on IQueryable<T>, which generally accept Expression<Func<...>> for some combination. So a basic select with an optional predicate would be:

        public IQueryable<T> Get<T>(         Expression<Func<T,bool>> predicate         ) where T : class     {         IQueryable<T> query = (IQueryable<T>)GetTable(typeof(T));         if (predicate != null) query = query.Where(predicate);         return query;     } 

    I would tend to return IQueryable<T> too, since that is fully composable. If the caller wants a list, they can always use ToList() on it… or (for example):

        using(var ctx = new MyDataContext(CONN))     {         ctx.Log = Console.Out;         int frCount = ctx.Get<Customer>(c => c.Country == 'France').Count();     } 

    which (using Northwind) does the query:

    SELECT COUNT(*) AS [value] FROM [dbo].[Customers] AS [t0] WHERE [t0].[Country] = @p0 

    The problem with including the ‘select’ (projection) in the query is that you would end up with multiple generic types. Since you often want the projection the be an anonymous type, it would then be pretty impossible to specify the projection type (anonymous) and the table-type, and it would not be callable.

    In reality, I wonder if there is much benefit writing such a method at all. I might just stick with a base method:

        public IQueryable<T> Get<T>() where T : class     {         return (IQueryable<T>)GetTable(typeof(T));     } 

    And let the caller compose it in their preferred way – perhaps with query syntax:

           var list = (from cust in ctx.Get<Customer>()                    where cust.Country == 'France'                    select cust.CompanyName).Take(10).ToList(); 

    Which uses:

    SELECT TOP (10) [t0].[CompanyName] FROM [dbo].[Customers] AS [t0] WHERE [t0].[Country] = @p0 

    Alternatively, if you really do want to include the order by and projection, then an extension method is the most practical approach; then you don’t need to specify the original (source) T (which is what makes it uncallable when mixed with anon-types):

    public static class QueryExtension {     public static IQueryable<TProjection>         Get<TSource, TProjection, TOrderKey>(             this IQueryable<TSource> source,             Expression<Func<TSource, bool>> where, // optional             Expression<Func<TSource, TProjection>> select,             Expression<Func<TProjection, TOrderKey>> orderBy)     {         if (where != null) source = source.Where(where);         return source.Select(select).OrderBy(orderBy);     } } 

    Then consider a DAL method such as:

        public List<string> Countries()     {         return Customers.Get(             x=>x.CompanyName != '',             x=>x.Country,             x=>x).Distinct().ToList();     } 

    Which uses (again, with Northwind):

    SELECT DISTINCT [t0].[Country] FROM [dbo].[Customers] AS [t0] WHERE [t0].[CompanyName] <> @p0 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: pass parameter in g:remoteLink as result of javascript function I am trying
Is it possible to pass a request from java servlet and read that request
Is it possible to pass *args to string.format? I have the following function: @classmethod
I've been looking for some time now.. Is is possible to pass easing function
I want to basically create a animation, and when I pass in an integer
I have a sql function that does a simple sql select statement: CREATE OR
Is it possible to pass a function as parameter to a method and evaluate
In PHP I've build a webpage that uses include() for loading parts of the
If I have a PHP function that generates a random number, is it possible
Possible Duplicate: Pass a PHP string to a Javascript variable (including escaping newlines) Hy,

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.