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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:30:40+00:00 2026-05-27T18:30:40+00:00

I have some code that currently builds up an In statement in SQL. I

  • 0

I have some code that currently builds up an In statement in SQL. I build an expression, it returns;

value(generic(list[T])).Contains(x => x.Id)

This works fine, so if I have a list of objects;

public class ObjectToSearch 
{
  public int IdToSearchOn {get;set;}
}

And I want to search for ids 1, 2, 3, it works just fine. My SQL query is great.

Now, I have a need to search a list of nested objects, so I might have;

public class ParentObjectToSearch 
{
  public IEnumerable<ObjectToSearch> Objects {get;set;}
}

So, looking at some code I found (How do I create an expression tree calling IEnumerable<TSource>.Any(…)?) I figured I could adapt the method, and wrap a call to Any or All, and it would work. This worked great, until I actually came to test against the database, I get;

Cannot compare elements of type 'System.Collections.Generic.ICollection`1'. Only primitive types (such as Int32, String, and Guid) and entity types are supported.

var collectionType = GetIEnumerableImpl( forCollection.Type );

Type elementType = collectionType.GetGenericArguments( )[0];

MethodInfo method = BaseFilter.GetType( ).GetMethod( "FilterWith" );

MethodInfo genericMethod = method.MakeGenericMethod( new[] { elementType } );

return (genericMethod.Invoke( BaseFilter, null ) as LambdaExpression);

FilterWith is the method I’m calling on the original filter, in the hope of getting back my expression. So, it looks like my inner expression is being evaluated incorrectly when combined with the outer expression. What I’m basically aiming for is (I believe);

x => x.Collection.Contains( y => new { 1, 3, 3}.Contains( y.Id));

If I test the inner filtering separately, it works fine, so I assume it’s just how I’m trying to combine the elements, and if I try and use Contains instead of Any or All, I still get the same error.

I’ve put Entity Framework in the tags, because this is being evaluated as expressions against an entity set, and someone may have experience of doing this.

Update Having a night to think about it, I think I have a better question;

How do I build a Where expression, so I can build;

x => x.Collection.Where( y => new[] { 1, 3 }.Contains( y.Id)).Count( ) > 0

  • 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-27T18:30:41+00:00Added an answer on May 27, 2026 at 6:30 pm

    The original error was actually being caused by the fact that I was trying to do check of null against the collection, well, that certainly can’t be done in SQL.

    Then, Any and All can’t be converted to SQL expressions, so;

    Expression<Func<TEntity, bool>> result = Expression.Lambda<Func<TEntity, bool>>(
                Expression.GreaterThan(
                    Expression.Call( CountMethod( elementType ),
                                    Expression.Call( WhereMethod( elementType ),
                                                    theCollectionWeAreSearching,
                                                    filter ) ),
                    Expression.Constant( 0 ) ), param );
    

    The elementType is the type of the element within the collection. The filter is an expression that tests my list. The Count and Where methods retrieved as follows;

    public MethodInfo GetMethodFromEnumerable(string methodName, params Func<MethodInfo, bool>[] filters)
            {
                var methods = typeof( Enumerable )
                    .GetMethods( BindingFlags.Static | BindingFlags.Public )
                    .Where( mi => mi.Name == methodName );
    
                methods = filters.Aggregate( methods, (current, filter) => current.Where( filter ) );
    
                return methods.First( );
            }
    
            public MethodInfo WhereMethod(Type collectionType)
            {
                // Get the Func<T,bool> version
                var getWhereMethod = GetMethodFromEnumerable( "Where",
                                        mi => mi.GetParameters( )[1].ParameterType.GetGenericArguments( ).Count( ) == 2 );
    
                return getWhereMethod.MakeGenericMethod( collectionType );
            }
    
            public MethodInfo CountMethod(Type collectionType)
            {
                var getCountMethod = GetMethodFromEnumerable( "Count" ); // There can be only one
    
                return getCountMethod.MakeGenericMethod( collectionType );
            }
    

    I think that what happened was the introduction of so much new code led me to look for problems where there weren’t any!

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

Sidebar

Related Questions

I currently have some code that pulls down a list of users in a
I currently have some code that will produce a crash dump when my application
I've been trying to implement unit testing and currently have some code that does
I have some code that gets the current logged in user. userID = request.session.get(_auth_user_id)
I have some Python code that works correctly when I use python.exe to run
I have some code that gives a user id to a utility that then
I have some code that uses the shared gateway pattern to implement an inversion
I have some code that raises PropertyChanged events and I would like to be
I have some code that looks like: template<unsigned int A, unsigned int B> int
I have some code that generates image of a pie chart. It's a general

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.