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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:10:14+00:00 2026-05-17T20:10:14+00:00

Everyone projects once in a while: var c = dc.Products.Select( p => new {p.id,

  • 0

Everyone projects once in a while:

 var c = dc.Products.Select( p => new {p.id, p.name});
 // results in c having properties of .id and .name

What if I want to refactor that in to a method. How would I pass the Select parameter?

var c = myFunction( p => new {p.id, p.name});

Product myFunction( ??? myLambda)
{
    var c = dc.Products.Select( myLambda);

    var result = new Product();
    foreach( var myproperty in //property in myLambda)
    {
        result.XX // property that matches, set it equal to myproperty
    }
}

Obviously, I need to figure out reflection a lot more. 🙂

  • 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-17T20:10:15+00:00Added an answer on May 17, 2026 at 8:10 pm

    The type of the parameter should be something like Expression<Func<Product,object>>. Also, I think you need your function to return an IEnumerable<Product> with the properties set.

    IEnumerable<Product> myFunction( Expression<Func<Product,object>> selector )
    {
        var products = new List<Product>();
        foreach (var c in dc.Products.Select( selector ))
        {
            var product = new Product();
            foreach (var property in c.GetType().GetProperties())
            {
                var productProperty = typeof(Product).GetProperty( property.Name );
                productProperty.SetValue( product, property.GetValue( c, null ) );
            }
            products.Add( product );
        }
        return products;
    }
    

    Having said that I think I would probably have a separate model for each combination of properties being returned and use a generic method to return one of those.

    IEnumerable<T> ProductPartial<T>( Func<Product,T> selector ) where T : new
    {
          return dc.Products.ToList().Select( selector );
    }
    

    used as

    var pricing = repository.ProductPartial( p => new PricingModel( p ) );
    

    Note: After thinking about it a little, if you are going to return a separate model, I think you may need to realize the query before you can do the conversion to the model anyway. In that case a Func<Product,T> is probably most appropriate — I’ve thrown an explicit ToList() in to make sure that the query is realized first so the select doesn’t fail. If it were a Where clause, that would be different. Even though this is slightly less efficient from a data transfer perspective (you’re retrieving all the properties), I still prefer using using the specific model if you feel like you need to put the selection in the repository. Since you’re only selecting part of the properties it feels wrong to pretend that you have an actual Product instance when you really don’t.

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

Sidebar

Related Questions

I am having a problem with NetBeans projects. It affects everyone on my team,
Everyone managing open-source-software runs into the problem, that with the time the process of
I'm having a problem designing part of my program (not writing it, for once!).
I have a problem in my project with the .designer which as everyone know
Everyone remembers google browser sync right? I thought it was great. Unfortunately Google decided
Everyone I work with is obsessed with the data-centric approach to enterprise development and
Everyone has accidentally forgotten the WHERE clause on a DELETE query and blasted some
Everyone (at least everyone who uses a compiled language) has faced compilation errors but
Everyone has this huge massively parallelized supercomputer on their desktop in the form of
Everyone in my office uses Macs and therefore most use Safari. We have 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.