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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:11:12+00:00 2026-05-13T00:11:12+00:00

I have an extension method with the following signature: public static Expression<Func<T, bool>> And<T>(this

  • 0

I have an extension method with the following signature:

public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
    ...
}

I have written a test-case for it that makes sure the two expressions are in fact combined. At least so that the new expression I get works.

Now I would like to write another test-case that just makes sure that the method uses the short-circuiting version of and. Any clue how I can do this?

I thought I could just do something like this:

    [Test]
    public void And_PredicatesAreShortCircuited()
    {
        var predicateNotUsed = true;
        Expression<Func<int, bool>> a = x => false;
        Expression<Func<int, bool>> b = x =>
            {
                predicateNotUsed = false;
                return true;
            };

        var foo = new[] { 1, 2, 3, 4, 5, 6, 7 }
            .Where(a.And(b).Compile())
            .ToArray();

        Assert.That(predicateNotUsed);
    }

But I get a giant red squiggly under that whole statement body for b stating that “A lambda expression with a statement body cannot be converted to an expression tree”. So… any options? Or is this an impossible test to write?

  • 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-13T00:11:12+00:00Added an answer on May 13, 2026 at 12:11 am

    Simple suggestion: use a reference type instead of a value type, and dereference it in the path you don’t want followed. Pass in null, and see whether it throws an exception 🙂

    [Test]
    public void And_PredicatesAreShortCircuited()
    {
        Expression<Func<string, bool>> a = x => false;
        Expression<Func<string, bool>> b = x => x.Length > 10;
    
        var foo = new[] { null, null }
            .Where(a.And(b).Compile())
            .ToArray();
    }
    

    Another alternative would be to use some side-effecting function on the input data (e.g. pass in something that can be changed by the expression tree) but I think the above will probably be the simplest approach 🙂

    Or another idea:

    public T NonVoidFail(T x)
    {
        Assert.Fail("I shouldn't be called");
        return x; // Will never happen
    }
    

    Then:

    [Test]
    public void And_PredicatesAreShortCircuited()
    {
        Expression<Func<int, bool>> a = x => false;
        Expression<Func<int, bool>> b = x => NonVoidFail(x);
    
        var foo = new[] { 1, 2, 3, 4, 5, 6, 7 }
            .Where(a.And(b).Compile())
            .ToArray();
    }
    

    It’s the same principle, but it’ll give you a nicer exception 🙂

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

Sidebar

Related Questions

I have the following extension method: public static string ToPropertyName<T,E>(this Expression<Func<E, T>> propertyExpression) {
I have following extension method written: static public IQueryable<OutboundPattern> ByClientID(this IQueryable<OutboundPattern> qry, int clientID)
I have the following extension method public static bool IsValidCurrency(this string value) { CultureInfo
I have the following extension method: public static void ThrowIfArgumentIsNull<T>(this T value, string argument)
I have an extension method with the following signature (in BuildServerExtensions class):: public static
I have the following extension method public static T Field<T>(this DataRow row, string columnName)
I have the following method: public static TEventInvocatorParameters Until <TEventInvocatorParameters, TEventArgs>(this TEventInvocatorParameters p, Func<TEventArgs,
I have the following extension method public static class ListExtensions { public static IEnumerable<T>
Say I have an overloaded extension method with the following two signatures: public static
I have the following method: public static string UlList(this HtmlHelper helper, List<IEntity> entities, string

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.