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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:12:12+00:00 2026-05-28T01:12:12+00:00

With Dynamic LINQ, what changes need to be done to have fields of the

  • 0

With Dynamic LINQ, what changes need to be done to have fields of the given class?

For example, how can the following C# query be reproduced in DLinq:

var carsPartial = cars.Select(c => new {c.year, c.name, make = new maker() {name = c.make.name} }).ToList();

I have applied the changes mentioned in this answer https://stackoverflow.com/a/1468357/288747 to allow the return type to be the calling type rather than an anonymous type.

With the class definition is as follows (if it helps):

class car
{
    public int vid;
    public int odo;
    public int year;

    public string name;

    public maker make;
}

class maker
{
    public string name;
    public int firstYear;
}

The following doesn’t work (but I think is close, but still doesn’t work as I don’t have the changes necessary to the dynamic linq library, which is what I need):

var carsPartial = cars.Select("new(name, year, new maker() {name = make.name})").ToList();

But it fails at the new maker() { (as expected).

I’m sure I need to change the DynamicLibrary.cs to get this working and could do with some direction on how to alter it to achieve 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-05-28T01:12:12+00:00Added an answer on May 28, 2026 at 1:12 am

    UPDATE: I have turned my answer into a little bit more extensive blog post.

    I have not really ever used Dynamic Linq library, but I have taken a look at the DynamicLibrary.cs code and the change to support generating type classes provided in another stackoverflow question you provided link to in your question. Analyzing them all, it seems that the nested new-s should work out of the box in your configuration.

    However, it seems your query is not the correct Dynamic Linq’s language query. Note, that the query string for DLinq is not equivalent to C# and has its own grammar.

    The query should read out, I believe, the following:

    var carsPartial = cars.Select("new(name, year, new maker(make.name as name) as make)").ToList();
    

    EDIT:

    Rereading this stackoverflow question more carefully, I realizes, that it actually does not extend the Dynamic Linq’s language with the possibility for creating new strong-typed classes. They just put the result to the class specified as a generic parameter of Select() instead of specifying it in the query string.

    To obtain what you need you will need to revert their changes (get generic DLinq) and apply my changes, I have just verified to work:

    Locate the ParseNew method of ExpressionParser class and change it to the following:

        Expression ParseNew() {
            NextToken();
    
            bool anonymous = true;
            Type class_type = null;
    
            if (token.id == TokenId.Identifier)
            {
                anonymous = false;
                StringBuilder full_type_name = new StringBuilder(GetIdentifier());
    
                NextToken();
    
                while (token.id == TokenId.Dot)
                {
                    NextToken();
                    ValidateToken(TokenId.Identifier, Res.IdentifierExpected);
                    full_type_name.Append(".");
                    full_type_name.Append(GetIdentifier());
                    NextToken();
                }
    
                class_type = Type.GetType(full_type_name.ToString(), false);    
                if (class_type == null)
                    throw ParseError(Res.TypeNotFound, full_type_name.ToString());
            }
    
            ValidateToken(TokenId.OpenParen, Res.OpenParenExpected);
            NextToken();
            List<DynamicProperty> properties = new List<DynamicProperty>();
            List<Expression> expressions = new List<Expression>();
            while (true) {
                int exprPos = token.pos;
                Expression expr = ParseExpression();
                string propName;
                if (TokenIdentifierIs("as")) {
                    NextToken();
                    propName = GetIdentifier();
                    NextToken();
                }
                else {
                    MemberExpression me = expr as MemberExpression;
                    if (me == null) throw ParseError(exprPos, Res.MissingAsClause);
                    propName = me.Member.Name;
                }
                expressions.Add(expr);
                properties.Add(new DynamicProperty(propName, expr.Type));
                if (token.id != TokenId.Comma) break;
                NextToken();
            }
            ValidateToken(TokenId.CloseParen, Res.CloseParenOrCommaExpected);
            NextToken();
            Type type = anonymous ? DynamicExpression.CreateClass(properties) : class_type; 
            MemberBinding[] bindings = new MemberBinding[properties.Count];
            for (int i = 0; i < bindings.Length; i++)
                bindings[i] = Expression.Bind(type.GetProperty(properties[i].Name), expressions[i]);
            return Expression.MemberInit(Expression.New(type), bindings);
        }
    

    Then, find the class Res and add the following error message:

    public const string TypeNotFound = "Type {0} not found";
    

    Et voilà, you will be able to construct queries like:

    var carsPartial = cars.Select("new(name, year, (new your_namespace.maker(make.name as name)) as make)").ToList();
    

    Make sure, you include the full type name including the whole namespace+class path.

    To explain my change, it just checks if there is some identifier between new and opening parenthesis (see the added “if” at the begging). If so we parse full dot-separated class name and try to get its Type through Type.GetType instead of constructing own class in case of anonymous news.

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

Sidebar

Related Questions

I need to build a dynamic linq query with or operators. I have seen
In Linq Dynamic Query , Scott Guthrie shows an example Linq query: var query
I'm using dynamic LINQ to create a query from given columns the user selects
How do you write a dynamic Linq query for the following simple search criteria?
Typically a dynamic linq query with string can use a substitution value such as:
I'm using jqGrid and I have a problem getting Dynamic Linq to work. I
I am following Scott Gu's article to create a dynamic LINQ http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx He has
I am trying to find the best way to build a dynamic linq query
I have been trying to use dynamic LINQ to Entity in my application for
How can I use dynamic LINQ in Visual Studio 2008? I'm trying to use

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.