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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:46:51+00:00 2026-05-14T21:46:51+00:00

In SubSonic 3.04’s SimpleRepository, I cannot seem to perform a Contains operation within a

  • 0

In SubSonic 3.04’s SimpleRepository, I cannot seem to perform a Contains operation within a lambda expression. Here’s a trivial example:

SimpleRepository repo = new SimpleRepository("ConnectionString");

List<int> userIds = new List<int>();
userIds.Add(1);
userIds.Add(3);

List<User> users = repo.Find<User>(x => userIds.Contains(x.Id)).ToList();

I get the error message:

variable ‘x’ of type ‘User’ referenced from scope ”, but it is not defined

Am I missing something here, or does SubSonic not support Contains in lambda expressions? If not, how would this be done?

  • 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-14T21:46:52+00:00Added an answer on May 14, 2026 at 9:46 pm

    Since neither of these seem to work…

    x => guids.Contains(x.Guid)
    x => guids.Any(y => y == x.Guid)
    

    … we write a custom lambda expression builder that generates:

    x => x.Id == {id1} OR x.Id == {id2} OR x.Id == {id3}
    

    This is a trivial scenario, but demonstrates how GetContainsId<User>(ids, repo) will find all Users with an Id that matches something in the supplied list.

    public List<T> GetContainsId<T>(List<int> ids, SimpleRepository repo)
        where T : Record, new() // `Record` is a base class with property Id
    {
        ParameterExpression x = Expression.Parameter(typeof(T), "x");
        LambdaExpression expr;
        if (ids.Count == 0)
        {
            expr = Expression.Lambda(LambdaExpression.Constant(false), x);
        }
        else
        {
            expr = Expression.Lambda(BuildEqual(x, ids.ToArray()), x);
        }
    
        return repo.Find<T>((Expression<Func<T,bool>>)expr).ToList();
    }
    
    private BinaryExpression BuildEqual(ParameterExpression x, int id)
    {
        MemberExpression left = Expression.Property(x, "Id");
        ConstantExpression right = Expression.Constant(id);
        return Expression.Equal(left, right);
    }
    
    private BinaryExpression BuildEqual(ParameterExpression x, int[] ids, int pos = 0)
    {
        int id = ids[pos];
        pos++;
    
        if (pos == ids.Length)
        {
            return BuildEqual(x, id);
        }
    
        return Expression.OrElse(BuildEqual(x, ids, pos), BuildEqual(x, id));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SubSonic 3.0.0.3 and I can't seem to get the ActiveRecord .tt files
Is it possible to apply a WHERE clause on a SubSonic query? For example,
I am using the SubSonic SimpleRepository template for my application. I have created an
Does SubSonic.SqlQuery have a between/and for date ranges? If not, what would be the
Is it possible for subsonic to access dbo.sp_help_job?
Has anyone managed to get subsonic or a variant working on Windows Mobile? We
I'm trying to use SubSonic for a new project with an existing database but
I am trying to pass some Subsonic collections to a client via a web
I've downloaded the source for SubSonic via SVN. When I try to open the
I was trying to TDD using SubSonic 2.1, MySQL and C# and when I

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.