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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:54:27+00:00 2026-05-17T22:54:27+00:00

I have this: List<string> fields; fields[0] = firstFieldName; fields[1] = secondFieldName; … fields[n] =

  • 0

I have this:

List<string> fields;

fields[0] = "firstFieldName";
fields[1] = "secondFieldName";
...
fields[n] = "nthFieldName";

I want to get this:

var selector = p => new {p.firstField, p.secondField, ..., p.nthFieldName}

// selector is of type Expression<Func<Entity, object>>

GoofBallLogic had this code that was simliar, ending up with p => p.column

// Entity is an object in a diagram from Entity Framework 4
var p = Expression.Parameter(typeof(Entity, "p");

var selector = Expression.Lambda<Func<Entity, string>(
    Expression.Property(p, columnToGroupBy), p );

EDIT: What I am trying to accomplish

I have a “generic” Repository:

public class Repository<E, C> : IRepository<E,C>
{
    private C _dc {get;set;} // ObjectContext (Entity Framework 4)
    private string _entityName {get;set;}
    public string entityKeyName {get;private set;}
    public List<string> entityKeys {get;private set;}
    public Expression<Func<E, object>> entityKey {get;private set;}
    private EntityContainer _containerName {get;set;}

    public Repository(C myDC)
    {   _dc = myDC; // TODO: check for null

        // Name of "this" ObjectContext
        _containerName = _dc.MetadataWorkspace.GetEntityContainer(
            _dc.DefaultContainerName, DataSpace.CSpace);

        // Name of "this" Entity
        _entityName = _containerName.BaseEntitySets
            .Where(p => p.ElementType.Name == typeof (E).Name)
            .Select( p => p.Name).FirstOrDefault();

        // String list of the keys
        entityKeys = _containerName
            .BaseEntitySets.First(meta => meta.ElementType.Name == 
                typeof(E).Name)
            .ElementType.KeyMembers.Select(k => k.Name).ToList();

        // Thanks Jon Skeet for this cool comma sep list formula
        entityKeyName = string.Join(",", entityKeys.ToArray() );  

        entityKey = Expression.Lambda<Func<E, object>> ... 

What to do to set entityKey as an object that can be used in
an OrderBy statement since Linq to Entities requires
ordering a set before doing a .Skip().Take()

Edit:

Amazingly, Orderby can take this:

p => "field1,field2,field3"

Which allows my code to execute but doesn’t actually order the items by the field values. It’s a first step in TDD I guess: use a literal.

  • 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-17T22:54:28+00:00Added an answer on May 17, 2026 at 10:54 pm

    I found this an interesting problem and took some time to figure it out, and found a relatively easy way to do it.

    Anyhow, here’s an example on how to do a single field sort (i’ll use your first field), if you want to sort on more fields you’ll have to create expressions for them too and use .ThenBy(xxx) after the usual OrderBy(xxx).

        // Create a parameter which passes the object
        ParameterExpression param = Expression.Parameter(typeof(E), "a");
    
        // Create body of lambda expression
        Expression body = Expression.PropertyOrField(param, fieldname);
    
        // Create lambda function
        Expression<Func<E, string>> exp = Expression.Lambda<Func<E, string>>(body, param);
    
        // Compile it so we can use it
        Func<E, string> orderFunc = exp.Compile();
    

    Now you can do an OrderBy(orderFunc) and it’ll sort the list by the property named in fieldname. Only downside being it only works for string fields (return value of expression). Could probably work around that too though.

    Fixed to work with any IComparable type:

            // Create a parameter which passes the field
            ParameterExpression param = Expression.Parameter(typeof(E), "a");
    
            // Create body of lambda expression
            Expression body = Expression.TypeAs(Expression.PropertyOrField(param, fieldname), typeof(IComparable));
    
            // Create lambda function
            Expression<Func<E, IComparable>> exp = Expression.Lambda<Func<E, IComparable>>(body, param);
    
            // Compile it so we can use it
            Func<E, IComparable> orderFunc = exp.Compile();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.