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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:05:54+00:00 2026-06-04T02:05:54+00:00

Can I make my EF objects retrieve only specific columns in the sql executed?

  • 0

Can I make my EF objects retrieve only specific columns in the sql executed?

If I have a column that contains a large amount of data that really slows down the query, how can I have my objects exclude that column from the sql generated?

If my table has Id(int), Name(int), Data(blob), how can I make my query be

select Id, Name from TableName

instead of

select Id, Name, Data from TableName

From the suggestion below, my method is

 public List<T> GetBy<T>(DbContext context,Expression<Func<T, bool>> exp, Expression<Func<T,T>> columns) where T : class
    {

        return dbContext.Set<T>().Where(exp).Select<T,T>(columns).ToList();
    }

And I’m calling it like so

List<CampaignWorkType> list = GetBy<CampaignWorkType>(dbContext, c => c.Active == true, n => new { n.Id, n.Name });

i got an error like below.

Cannot implicitly convert type ‘AnonymousType#1’ to ‘Domain.Campaign.CampaignWorkType’

how i can solve this?

  • 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-04T02:05:55+00:00Added an answer on June 4, 2026 at 2:05 am

    The solution is:

    First, define a surrogate type:

    public class CampaignWorkTypesSimpleList
    {
       public int Id { get; set; }
       public string Name { get; set; }
    }
    

    Then change generic method like this:

    public List<U> GetBy<T,U>(DbContext context,Expression<Func<T, bool>> exp, Expression<Func<T,U>> columns) 
                    where T : class
                    where U : class
    {
    
      return dbContext.Set<T>().Where(exp).Select<T, U>(columns).ToList();
    }
    

    Finally, execute it.

    List<CampaignWorkTypesSimpleList> list = this.GetBy<CampaignWorkType, CampaignWorkTypesSimpleList>(dbContext, c => c.Active == true, n => new CampaignWorkTypesSimpleList { Id = n.Id, Name = n.Name });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I make my EF objects retrieve only specific columns in the sql executed?
I have a class that contains multiple user objects and as such has an
I need to make some changes so that I only retrieve one object at
Since there is no way that you can make the flash object transparent, there
Given an instantiated JavaScript object/class can I make an Ajax call from that class
How can make a link within a facebox window that redirects it to another
I can make a log in for easily, so that's not the problem. What
How can you make youtube requests without a developer key? eg i have: Uri
I know that you can store a bunch of Parent* objects in a vector.
I have multiple linq queries that retrieve the same data just at different grouping

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.