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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:00:23+00:00 2026-05-15T17:00:23+00:00

I’m using LINQ-to-Entities. Using the following query: var x = from u in context.Users

  • 0

I’m using LINQ-to-Entities. Using the following query:

var x = from u in context.Users select new { u.Id, u.Name };

That only selects the Id and Name columns. Great.

I’m trying to make a repository that can be passed that new { u.Id, u.Name} as a parameter to allow the client to pick which columns are used in the SELECT statement.

I’m trying to use the “fluent” interface with my repository instead of the query syntax. Basically the final call is to convert IQueryable to IList and actually execute the code. I’ve looked at the DynamicExpressions library but I’m not really sure this is the route I want to take for this. It’s possible to do it with the query syntax, but not the fluent interface?

Edit: Sorry I should have mentioned that all queries are encapsulated inside the repository. So for example I want to be able to expose a method like the following:

public void Project(Expression<Func<TEntity, TEntity>> fields)
{
    this.Projection = fields;
}

So calling this would be like:

using (DBContext context = new DBContext())
{
    IUserRepository repo = new UserRepository(context);
    repo.Project(u => new { u.Id, u.Name });
    repo.GetById(100);
}

Inside of GetById would be something like ctx.Users.Where(u => u.Id == id).Select(this.Projection).

This way, which columns are returned can be selected by the calling code. The reason for this is because maybe I want to return a User object but maybe I only need the Id and Name (and thus returning less data over the wire).

The problem is that I obviously can’t convert the anonymous type to User. It would be cool if I could do something like:

repo.Project(u => new User() { Id = u.Id, Name = u.Name });

Which would mean I don’t need to create an anonymous on type. EDIT: Ok this seems to work ONLY if the type that’s returned is a POCO … ugh.

EDIT2: I might have to go the DLINQ approach. What I’m thinking is having Expression<Func<TEntity, object>> (to use the anonymous type) and then use reflection to get the list of properties. Then, using DLINQ build a string expression. The only downside is that I don’t really want to use DLINQ as it adds a little overhead (Reflection.Emit etc..).

  • 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-15T17:00:24+00:00Added an answer on May 15, 2026 at 5:00 pm

    I’m afraid I’m not quite sure what you’re actually asking, but that query is easy to write using dot notation (the term I use for what I think you call fluent interface):

    var x = context.Users.Select(u => new { u.Id, u.Name });
    

    As Adam says, if you’re trying to create something which takes this tuple as a value in a strongly typed way, you’re going to need a named type. You can either create your own, if you’re using .NET 4 you could use Tuple<T1, T2>.

    That doesn’t seem to fit with the bit of your question about claiming you can do what you want with a query expression though… could you elaborate?

    EDIT: Okay, now I’ve got some idea of what you’re talking about…

    … you should make the Project method take an expression tree of type Expression<Func<TInput, TOutput>> and return something which uses TOutput – e.g. a RepositoryProjection<TOutput>. That way the anonymous type is still effectively captured within your code – so you can use it later (e.g. to put an extra condition on the query).

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

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I have a bunch of posts stored in text files formatted in yaml/textile (from
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

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.