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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:48:10+00:00 2026-06-05T19:48:10+00:00

I have a situation where I need to dynamically build up a list of

  • 0

I have a situation where I need to dynamically build up a list of filters to apply to a list of objects. Those objects can be anything that implements an interface which contains all of the properties I need to filter on.

public interface IStuff
{
    bool SuitableForSomething { get; set; }
    bool SuitableForSomethingElse { get; set; }
}

public class SomeStuff : IStuff
{
    ...
}

public class SomeOtherStuff : IStuff
{
    ...
}

I have a list of criteria defined like so …

public List<Expression<Func<IStuff, bool>>> Criteria { get; private set; } 

and add criteria like so …

Criteria.Add(x => x.SuitableForSomething);
Criteria.Add(x => x.SuitableForSomethingElse);

I then apply the criteria to my query like so …

var stuff= _stuffCache
    .GetAll()
    .AsQueryable()
    .ApplyCriteria(Criteria);

which uses the following extension method …

public static IQueryable<T> ApplyCriteria<T>(this IQueryable<T> stuff, List<Expression<Func<IStuff, bool>>> criteria) 
    where T : IStuff
{
    foreach (var expression in criteria)
    {
        stuff = Queryable.Where(stuff, expression);
    }
    return stuff;
}

The compiler is telling me …

cannot convert from
    'System.Linq.Expressions.Expression<System.Func<IStuff,bool>>'
to
    'System.Linq.Expressions.Expression<System.Func<T,int,bool>>'

When I hover over the red line under the error in the IDE it’s saying it cannot resolve method between

    IQueryable<IStuff> Where<IStuff>(this IQueryable<IStuff>, Expression<Func<IStuff, bool>>) in class Queryable
and
    IQueryable<T> Where<T>(this IQueryable<T>, Expression<Func<T,int,bool>>) in class Queryable

If I try casting the expression to Expression<Func<T, bool>>, which ought to work as T is constrained to implement the IStuff interface. I get

Cannot cast expression of type 'Expression<Func<IStuff, bool>>' to type 'Expression<Func<T, bool>>'

EDIT
Thanks to Raphaël‘s answer I fixed the extension method and eventually found the real problem I had, which was a casting problem when I was calling the code. Easily fixed by adding a .Cast<SomeStuff>() after the ApplyCriteria call.

Before

var stuff= _stuffCache
    .GetAll()
    .AsQueryable()
    .ApplyCriteria(Criteria);

After

var stuff= _stuffCache
    .GetAll()
    .AsQueryable()
    .ApplyCriteria(Criteria)
    .Cast<SomeStuff>();
  • 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-05T19:48:12+00:00Added an answer on June 5, 2026 at 7:48 pm

    change second parameter type to List<Expression<Func<T,bool>>> (T instead of IStuff)

    public static IQueryable<T> ApplyCriteria<T>(this IQueryable<T> stuff, List<Expression<Func<T, bool>>> criteria) 
        where T : IStuff
    {
        foreach (var expression in criteria)
        {
            stuff = Queryable.Where(stuff, expression);
            //or stuff = stuff.Where(expression), as Where is an Extension method;
        }
        return stuff;
    }
    

    and your method can be (thx resharper), rewritten to

    public static IQueryable<T> ApplyCriteria<T>(this IQueryable<T> stuff, List<Expression<Func<T, bool>>> criteria)
                where T : IStuff
            {
                return criteria.Aggregate(stuff, (current, expression) => current.Where(expression));
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation whereby, i need to create buttons dynamically and i need
In a Flex Mobile Application, I have a situation where I need to dynamically
I have a situation where I need to generate the below XML dynamically in
I have a situation where I need to dynamically disable certain controls. I will
I have a situation where I need to dynamically load a URL prefix. I
Ok, so I have the following situation. I need to edit the PYTHONPATH dynamically
Here's the situation: I have a menu that needs to be created dynamically from
I have situation where I need to change the order of the columns/adding new
I have a situation where I need to update a control referenced in a
I have a situation where I need to notify some users when something in

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.