简体   繁体   中英

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

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();

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM