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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:30:57+00:00 2026-05-25T12:30:57+00:00

I am attempting to build a simple Where clause. This is the code that

  • 0

I am attempting to build a simple Where clause.

This is the code that does not work:

EDIT this code works fine now (thanks to answers below).

public class Item
{
    public int Value { get; set; }
    public string Name { get; set; }
}

var _List = new List<Item>
{
    new Item{ Name = "Smith", Value = 1},
    new Item{ Name = "Smith", Value = 2},
    new Item{ Name = "Wesson", Value = 3},
    new Item{ Name = "Wesson", Value = 4},
};

// Where(x => x.Value == 1)
var _Type = typeof(Item);
var _Prop = _Type.GetProperty("Value");
var _Param = Expression.Parameter(_Type, _Prop.Name);
var _Left = Expression.PropertyOrField(_Param, _Prop.Name);
var _Right = Expression.Constant(1, _Prop.PropertyType); 
var _Body = Expression.Equal(_Left, _Right);
var _Where = Expression.Lambda<Func<Item, bool>>(_Body, _Param); 
var _Result = _List.AsQueryable().Where(_Where);

Thank you.

  • 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-25T12:30:58+00:00Added an answer on May 25, 2026 at 12:30 pm

    There are several problems with your code:

    1. You need to pass 1 and not "1" for the integer constant 1.

      var _Right = Expression.Constant(1, _Prop.PropertyType);
      
    2. Expression.Equals if two expression trees are equal. It returns a bool.

      Expression.Equal returns an expression tree that represents an equality check.

      var _Body = Expression.Equal(_Left, _Right);
      
    3. The parameter is of type Item and not int.

      var _Where = Expression.Lambda<Func<Item, bool>>(_Body, _Param);
      
    4. The List<T> implements IEnumerable<T>, but not IQueryable<T>.

      IEnumerable<T> works with delegates, while IQueryable<T> works with expression trees.

      So you need to either compile your expression tree to a delegate

      var _Where = Expression.Lambda<Func<Item, bool>>(_Body, _Param).Compile();
      var _Result = _List.Where(_Where);
      

      or convert the list to IQueryable<T>.

      var _Where = Expression.Lambda<Func<Item, bool>>(_Body, _Param);
      var _Result = _List.AsQueryable().Where(_Where);
      

    Working code:

    // Where(x => x.Value == 1)
    var _Param = Expression.Parameter(typeof(Item), "x");
    var _Left = Expression.PropertyOrField(_Param, "Value");
    var _Right = Expression.Constant(1);
    var _Body = Expression.Equal(_Left, _Right);
    var _Where = Expression.Lambda<Func<Item, bool>>(_Body, _Param).Compile();
    var _Result = _List.Where(_Where);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to build a simple method that creates an XML file from
I'm attempting to build a simple plugin like this (function($) { $.fn.gpSlideOut = function(options,
I'm attempting to build some C++ code that requires the Windows 7.0 SDK header
I'm attempting to build a very simple wxPython GUI that monitors and displays external
I'm using System.Linq.Expressions I was attempting to build a simple LambdaExpression that includes a
I am attempting to build a simple WPF Application using msbuild via a custom
I'm attempting to solve the Code Golf: Build Me an Arc problem. My solution's
I am attempting to build a simple C# TCP proxy for my business so
Note: This is for a shop that works in C++, C++/CLI, and C# with
I'm attempting to build a queue of queues that I can use to manage

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.