Home > front end >  How to check whether x => (id == null || id == x) is a tautology x => true?
How to check whether x => (id == null || id == x) is a tautology x => true?

Time:07-12

For id = null, my expression x => (id == null || x == id) should be considered as a tautology x => true.

As I am not knowledgeable in Expression my code below fails to detect the tautology correctly. The program output says that it is not tautology.

Question:

What is the proper way to detect it?

using Microsoft.EntityFrameworkCore.Query;
using System.Linq.Expressions;

int? id = null;


Expression<Func<int, bool>> criteria = x => (id == null || x == id);
Expression<Func<int, bool>> tautology = x => true;


if (ExpressionEqualityComparer.Instance.Equals(criteria, tautology))
    Console.WriteLine("tautology");
else
    Console.WriteLine("not tautology");

Console.ReadKey();

CodePudding user response:

While it can mean the same thing it does not make the expressions equal. You can check the sources of ExpressionEqualityComparer yourself - it compares expression without optimization/reduction as is. And the compiler generated expression trees will definitely will be different.

  • Related