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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:42:33+00:00 2026-05-30T07:42:33+00:00

First, I must say that I have read several post about this at StackOverflow

  • 0

First, I must say that I have read several post about this at StackOverflow but I cannot get the desired result.

Let me explain the context (simplified): I’m using Linq-to-SQL to query customers with recent visits to the store and (optionally) get only those with certain amount of payments. Suppose I have a model with client, visits and payments classes.

So, focusing on the Where expression, I’m trying this:

Expression<Func<Entityes.Clients, bool>> filter = null;

filter = c => 
          c.Visits.Any(v => v.VisitDate > DateTime.Now.Date.AddMonths(-(int)visitsSince));


if (minPayment.HasValue && minPayment.Value > 0)
{
     filter.And(
               c => c.Payments.Sum(p => p.Quantity) > minPayment.Value);
}

The filter.And method is an extension method, recommended at this forum, below you can see the definition:

public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expression1,
                                            Expression<Func<T, bool>> expression2)
{
  InvocationExpression invokedExpression =
      Expression.Invoke(expression2, expression1.Parameters.Cast<Expression>());

  return Expression.Lambda<Func<T, bool>>
      (Expression.And(expression1.Body, invokedExpression), expression1.Parameters);
}

However, this doesn’t work for me as expected and results are not filtered by payment amount. There is no error with data or linq-to-sql model because this code works fine:

filter = c => 
           c.Visits.Any(v => v.VisitDate > DateTime.Now.Date.AddMonths(-(int)visitsSince)))
             && c => c.Payments.Sum(p => p.Quantity) > minPayment.Value;

However, I don’t want to use the last code because I have some more complex scenarios in which I need to build the filter expression step-by-step with an ‘and/or’ combination of each part.

PD: I’m a “ADO.Net DataSets” guy trying to learn Linq-to-SQL and soon Entity Framework, I hope to be useful to the StackOverflow community soon.

  • 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-30T07:42:34+00:00Added an answer on May 30, 2026 at 7:42 am

    You are not assigning the result of Anding to anything.

    I suppose the respective part of your code should read as following:

    if (minPayment.HasValue && minPayment.Value > 0)
    {
       filter = filter.And(
                c => c.Payments.Sum(p => p.Quantity) > minPayment.Value);
    }
    

    The thing is that your (or SO’s as you mentioned) And function returns the expression containing the conjunction of two expression. It does not alter the object it is invoked on.

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

Sidebar

Related Questions

First of all I must say that this may seem like a lot of
first of all, i like to say that i have read the other two
First of all, I must say that I'm very new to Google Closure, but
First of all, I must say that I am quite new to the API
Well, first of all sorry about this question it must be pretty straight forward
First of all, I want to say sorry for this very big question, but
Good day, first i have to say that i'm mexican so, excuse my not
I first got an error usign the code below, explaining that DataGridLinkButton' must be
My colleague here argues that new programmers must learn ASP first, before they learn
Suppose that I have a huge SQLite file (say, 500[MB]) stored in Amazon S3

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.