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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:29:46+00:00 2026-06-08T16:29:46+00:00

Background I’m working with Microsoft Dynamics CRM 2011 QueryExpressions, which for all you non-CRM

  • 0

Background

I’m working with Microsoft Dynamics CRM 2011 QueryExpressions, which for all you non-CRM people out there, just know that I’m working with an SDK to access a database that requires you to put the string names of the columns you are selecting in a custom ColumnSet class:

new ColumnSet("personid", "name", "age");

The SDK does generate early bound classes so I do have classes for all the database tables, and the early bound classes all have a dictionary whose key are the columns of the table that have been populated on the object. ie:

var p = new Person { Name = "John", Age = 39, SSN = null };
p.Attributes.Count == 3;
// p.Attributes.Keys == { "name", "age", "ssn" };

The Problem

I have three issues when populating the ColumnSet

  1. I forget what columns a class/table contains
  2. I fat finger some of the columns and don’t get an error until runtime
  3. The column Names all have to be lower case when instantiating the ColumnSet, which makes for poor readability ie thissillyexampleisntthatreadable vs ThisSillyExampleIsntThatReadable

All three of these issues can be solved by early binding.

I know I can create an enum or stuct for each class that contains all of a class’s columns ie: new ColumnSet(PersonColumns.PersonId, PersonColumns.Name, PersonColumns.Age), but I want it to use the classes that have already been generated.

My Best Attempt

The best that I can currently come up with is this:

ColumnSetFactory.Create<Person>(p => p.PersonId = null, p.Name = null, p.Age = null);

Where Create accepts an object of type T (person in this example) and then inspects the dictionary of the object to generate and return the ColumnSet.

The Goal

Have a generic function that utilizes the early bound classes to generate the ColumnSet:

ColumnSetFactory.Create<Person>(p => p.PersonId, p.Name, p.Age);

Any ideas?

  • 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-08T16:29:50+00:00Added an answer on June 8, 2026 at 4:29 pm

    Your attempt/goal syntax does not work in C# unfortunately, but you could probably get something close to it.

    I’m not familiar with the dynamics crm so I’m making this assumption, the constructor for ColumnSet takes a variable amount of string arguments and has a signature:

    public ColumnSet(params string[] arguments)
    

    You could create a lambda expression that returns an object initializer (to create an anonymous object) and use some reflection to invoke the constructor using the initializer’s member bindings. If I understand what you are trying to accomplish, you want to use existing parameter names of an object to essentially pass those names to this constructor to create the object.

    Here’s how you can do that:

    public static ColumnSet Create<T>(Expression<Func<T, object>> parameters)
    {
        var initializer = parameters.Body as NewExpression;
        if (initializer == null || initializer.Members == null)
            throw new ArgumentException("lambda must return an object initializer");
        var memberNames = initializer.Members
            .Select(member => member.Name.ToLower())
            .ToArray();
        var ctor = typeof(ColumnSet).GetConstructor(new Type[] { typeof(string[]) });
        return (ColumnSet)ctor.Invoke(new object[] { memberNames });
    }
    

    Then to use it, call it like this:

    ColumnSetFactory.Create<Person>(p => new { p.PersonId, p.Name, p.Age });
    
    // Personally I prefer to call it like this to let the compiler
    // infer the generic arguments
    ColumnSetFactory.Create((Person p) => new { p.PersonId, p.Name, p.Age });
    

    Which would generate an equivalent call to the constructor:

    new ColumnSet("personid", "name", "age");
    

    You could even make up column names and give them random values, the values themselves will not be used, just the name of the member.

    ColumnSetFactory.Create<Person>(p => new
    {
        p.PersonId,
        p.Name,
        p.Age,
        Foobar = 0,
        Boo = "rawr!!!",
    });
    

    Which would generate an equivalent call to the constructor:

    new ColumnSet("personid", "name", "age", "foobar", "boo");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background Hi All, I'm trying to use Boost::MPI, at the moment I'm just trying
Background: I'm using the (fantastic) Vim plugin python-mode , which includes the pep8 linter.
Background : I'm trying to convert some JavaScript code which uses the the Crossfilter
Background I am working with a monad built of a stack of transformers one
Background I have a ror application which is continuously recording and showing on a
Background I graduated 6 months ago, and my collaborative work in college has all
Background: My employeer at my non-programming job knows that I am an undergraduate CS
Background: I am developing an app which sends an SMS to users after registration
Background: I have a website where people can store transactions. As part of this
BACKGROUND I'm using VS 2010 on a machine where I installed .Net 4.5 which

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.