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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:26:23+00:00 2026-05-11T08:26:23+00:00

Using dynamic linq ( http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx ) I run the query below which returns an

  • 0

Using dynamic linq (http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx) I run the query below which returns an IQueryable. On this result which is also an IEnumerable I am able to run a Count() however there is not ToList() to get the values. I can enumerate over them using a foreach but am not able to access the individual fields in the resultant anonymous type without reflection. Is this the only way? Thanks

var query =                 db.Customers.Where('City == @0 and Orders.Count >= @1', 'London', 10).                 OrderBy('CompanyName').                 Select('New(CompanyName as Name, Phone)');              foreach (var item in query)             {                 foreach (var prop in item.GetType().GetProperties())                 {                     Console.WriteLine(prop);                 }             } 

Tried casting but got an error Unable to cast object of type ‘DynamicClass1’ to type ‘Test’.

 foreach (var item in query)             {                 Console.WriteLine('{0}: {1}', ((Test)item).CompanyName, ((Test)item).Phone);             }  public class Test {             public string CompanyName { get; set; }             public string Phone { get; set; } 

}

  • 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. 2026-05-11T08:26:24+00:00Added an answer on May 11, 2026 at 8:26 am

    You can’t expect the type of you dynamic object to be resolved at compile time when your query is resolved at runtime. That’s what dynamic linq is all about : it allows you to execute dynamic queries at the cost compile-time checking.

    You have to use reflection to access properties of a dynamic type, at least until C# 4.

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

Sidebar

Related Questions

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 using the Dynamic Linq library found here: http://speakingin.net/2008/01/08/dynamic-linqparte-1-usando-la-libreria-de-linq-dynamic/ http://msdn.microsoft.com/en-us/vstudio//bb894665.aspx I have the
I am building a dynamic query builder using the dynamic linq library. SO far
I am trying to run an IN statement through Dynamic LINQ by using the
Using the Dynamic LINQ library ( link ), is it vulnerable to injection? and
I'm using the Dynamic.ParseLambda method from the Dynamic LINQ library to create expressions, compile
I am using Dynamic Data with linq to SQL and SQL Server 2008. I
Using LINQ , I've been trying to use the System.Linq.Dynamic library in order to
I'm using dynamic LINQ to create a query from given columns the user selects
I am using Dynamic Linq to query our database with a string that is

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.