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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:04:24+00:00 2026-05-31T11:04:24+00:00

This is linq-to-sql I have a lot of different classes all doing the same

  • 0

This is linq-to-sql

I have a lot of different classes all doing the same query, but projecting the results slightly differently. Ideally I’d like to be able to have the query in one place, and have the projection passed into the Select method. It works fine for concrete types:

public void GetResults() {
    var junk = db.SiteProducts.Select(Project());
}

public Expression<Func<DbEntities.SiteProduct, string>> Project() {
    return p => p.ProductAlert;
}

But when I try to return an anonymous type, it fails

public void GetResults() {
    var junk = db.SiteProducts.Select(Project());
}

public Expression<Func<DbEntities.SiteProduct, TResult>> Project<TResult>() {
    return p => new { p.ProductAlert };
}

I fully understand why generic type inference is failing in the second case. But is there a trick—short of crafting my own Expressions from the ground up—I’m missing that could get this to work?

  • 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-31T11:04:25+00:00Added an answer on May 31, 2026 at 11:04 am

    If I understand your question correctly you can use this code:

    first declare a method for selecting your data like this:

    public List<TResult> FindAll<TResult>(Func<Regions, TResult> selector) where TResult : class
        {
            using (RepositoryDataContext = new DataClasses1DataContext())
            {
                    return RepositoryDataContext.Regions.Select<Regions, TResult>(selector).ToList<TResult>();
    
            }
        }
    

    then you can build your select statement like this:

    Func<Regions, SelectAllRegion> select = r => new SelectAllRegion
            {
                RegionID = r.RegionID,
                RegionDescription = r.RegionDescription
            };
    

    my SelectAllRegion :

     public class SelectAllRegion
    {
        public SelectAllRegion()
        {
        }
        public int RegionID { get; set; }
        public string RegionDescription { get; set; }
    }
    

    and region is Region table in northwing.I hope this help you

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

Sidebar

Related Questions

This Linq to SQL query ... Return (From t In Db.Concessions Where t.Country =
How can I write a query similar to this one in LINQ to SQL
I am searching for the Linq-to-SQL equivalent to this query: SELECT [cnt]=COUNT(*), [colB]=SUM(colB), [colC]=SUM(colC),
i seem to be getting a lot of this in my Linq 2 SQL
I have a linq to sql query var items = from p in ctx.bam_Zending_AllInstances
I have an issue with a linq to sql query - performance wise. What
We have a program that uses Linq-To-Sql and does a lot of similar queries
I have created some extra functionality on my Linq-to-SQL classes to make things easier
I have this Linq to object query: var result = Users.Where(u => u.Address.Country.Code ==
I have not hardly touched EF4, but I've used Linq to sql quite a

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.