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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:05:39+00:00 2026-05-28T02:05:39+00:00

I am trying to use a dynamic linq query to retrieve an IEnumerable<T> from

  • 0

I am trying to use a dynamic linq query to retrieve an IEnumerable<T> from an object collection (Linq to Object), each of the objects in the collection have an internal collection with another set of objects where the data is stored, these values are accessed through an indexer from the outer collection

The dynamic linq query returns the filtered set as expected when you are working with strongly typed objects but my object stores the data in a member of type dynamic, please see the example below:

public class Data
{
    public Data(string name, dynamic value)
    {
        this.Name = name;
        this.Value = value;
    }

    public string Name { get; set; }
    public dynamic Value { get; set; }
}

public class DataItem : IEnumerable
{
    private List<Data> _collection;

    public DataItem()
    { _collection = new List<Data>(); }

    public dynamic this[string name]
    {
        get
        {
            Data d;
            if ((d = _collection.FirstOrDefault(i => i.Name == name)) == null)
                return (null);

            return (d.Value);
        }
    }

    public void Add(Data data)
    { _collection.Add(data); }

    public IEnumerator GetEnumerator()
    {
        return _collection.GetEnumerator();
    }
}

public class Program
{
    public void Example()
    {
        List<DataItem> repository = new List<DataItem>(){
            new DataItem() {
                new Data("Name", "Mike"),
                new Data("Age", 25),
                new Data("BirthDate", new DateTime(1987, 1, 5))
            },
            new DataItem() {
                new Data("Name", "Steve"),
                new Data("Age", 30),
                new Data("BirthDate", new DateTime(1982, 1, 10))
            }
        };

        IEnumerable<DataItem> result = repository.AsQueryable<DataItem>().Where("it[\"Age\"] == 30");
        if (result.Count() == 1)
            Console.WriteLine(result.Single()["Name"]);
    }

When I run the above example I get: Operator ‘==’ incompatible with operand types ‘Object’ and ‘Int32’

Are dynamic members incompatible with Dynamic Linq queries?, or is there another way of constructing expressions that would evaluate properly when dealing with members of type dynamic

Thanks a lot for your help.

  • 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-28T02:05:39+00:00Added an answer on May 28, 2026 at 2:05 am

    Are dynamic members incompatible with Dynamic Linq queries?, or is there another way of constructing expressions that would evaluate properly when dealing with members of type dynamic?

    Both can work together. Just do a conversion to Int32 before doing the comparison like so:

    IEnumerable<DataItem> result = 
               repository.AsQueryable<DataItem>().Where("Int32(it[\"Age\"]) == 30");
    

    Edit 1: Having said that, the use of dynamic binding in connection with Linq is restricted in general as dynamic operations are not allowed in expression trees. Consider the following Linq-To-Objects query:

    IEnumerable<DataItem> result = repository.AsQueryable().
                                                      Where(d => d["Age"] == 30);
    

    This code snippet won’t compile for the reason mentioned above.

    Edit 2: In your case (and in conjunction with Dynamic Linq) there are some ways to hack yourself around the issues mentioned in Edit 1 and in the original question. For example:

    // Variant 1: Using strings all the way
    public void DynamicQueryExample(string property, dynamic val)
    {
       List<DataItem> repository = new List<DataItem>(){
            new DataItem() {
                new Data("Name", "Mike"),
                new Data("Age", 25),
                new Data("BirthDate", new DateTime(1987, 1, 5))
            },
            new DataItem() {
                new Data("Name", "Steve"),
                new Data("Age", 30),
                new Data("BirthDate", new DateTime(1982, 1, 10))
            }
        };
    
        // Use string comparison all the time        
        string predicate = "it[\"{0}\"].ToString() == \"{1}\"";
        predicate = String.Format(whereClause , property, val.ToString());
    
        var result = repository.AsQueryable<DataItem>().Where(predicate);
        if (result.Count() == 1)
            Console.WriteLine(result.Single()["Name"]);
    }
    
    Program p = new Program();
    
    p.DynamicQueryExample("Age", 30); // Prints "Steve"
    p.DynamicQueryExample("BirthDate", new DateTime(1982, 1, 10)); // Prints "Steve"
    p.DynamicQueryExample("Name", "Mike"); // Prints "Steve" (nah, just joking...)
    

    or:

    // Variant 2: Detecting the type at runtime.
    public void DynamicQueryExample(string property, string val)
    {
        List<DataItem> repository = new List<DataItem>(){
            new DataItem() {
                new Data("Name", "Mike"),
                new Data("Age", 25),
                new Data("BirthDate", new DateTime(1987, 1, 5))
            },
            new DataItem() {
                new Data("Name", "Steve"),
                new Data("Age", 30),
                new Data("BirthDate", new DateTime(1982, 1, 10))
            }
        };
    
        string whereClause = "{0}(it[\"{1}\"]) == {2}";
    
    
        // Discover the type at runtime (and convert accordingly)
        Type type = repository.First()[property].GetType();
        string stype = type.ToString();
        stype = stype.Substring(stype.LastIndexOf('.') + 1);
    
        if (type.Equals(typeof(string))) {
            // Need to surround formatting directive with ""
            whereClause = whereClause.Replace("{2}", "\"{2}\"");
        }
        string predicate = String.Format(whereClause, stype, property, val);
    
        var result = repository.AsQueryable<DataItem>().Where(predicate);
        if (result.Count() == 1)
            Console.WriteLine(result.Single()["Name"]);
    }
    
    var p = new Program();
    p.DynamicQueryExample("Age", "30");
    p.DynamicQueryExample("BirthDate", "DateTime(1982, 1, 10)");
    p.DynamicQueryExample("Name", "Mike");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to use dynamic LINQ to Entity in my application for
I'm trying to use PredicateBuilder to compose dynamic linq queries. In my object, I
How can I use dynamic LINQ in Visual Studio 2008? I'm trying to use
Using LINQ , I've been trying to use the System.Linq.Dynamic library in order to
Is it possible to use a dynamic Linq Expression inside a Query Expression? Something
I am trying to use the C# Linq query Descendants method using variables. Here
I am trying to use the ASP.NET Dynamic Data features to generate CRUD scaffolding
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I am trying to use the Dynamic Data Display library for WPF in my
I'm trying to convert a List<Topic> to an anonymous or dynamic type via linq

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.