C# 表达式比较
C# Expression Comparison
假设我在集合中有以下表达式:
var people = new List<Person>
{
new Person {FullName = "Some Dude", Age = 45},
new Person {FullName = "Another Dude", Age = 28},
new Person {FullName = "Some Other Dude", Age = 36}
};
var filtered = people.Where(person => person.Age > 28 && person.FullName.StartsWith("So"));
var narrowlyFiltered = people.Where(person => person.Age > 36 && person.FullName.StartsWith("Some"));
有没有办法在运行时比较这两个表达式并推断出第二个表达式是第一个表达式的子集?无需枚举或其他任何内容。我只有表达式,我正在尝试找出这些表达式是否相交或包含彼此。
如果您可以枚举您的集合,您可以先将元素放在 HashSet<T>
中,然后 运行 将 HashSet<T>.IsSubSet
放在上面:
HashSet<T> hs = new HashSet<T>(filtered);
HashSet<T> hs2 = new HashSet<T>(narrowlyFiltered);
hs.IsSubSetOf(hs2); //<- booleans saying true or false
否则,这个问题一般undecidable problem。尽管有启发式方法可以适用于许多 情况。例如,您可以尝试使用旨在在编译时推断出这一点的代码契约。
证明:
The formal variant is: given two Turing machines (methods, delegates, pointers), does every string contained in the first language is contained in the second?
Undecidable
proof: given it was decidable, EQTM would be decidable: simply first validate whether the first Turing machine is a subset of the second and vice versa. If both are subsets, we know they accept the same set of strings.
换句话说,如果你能做到这一点,你也可以推断出两个函数是否产生相同的结果,which cannot be implemented。
您必须将每个 Expression 分解为所有可能的继承类型(MethodCallExpression、ConditionalExpression 等),然后并行进行每个分解并检查每个可能的参数...编码会有点长。 .. 你可以从 ExpressionEqualityComparer
中得到启发
看看Specification design pattern
一旦实施,您的规范在这种情况下就变成了
public class PersonNamedOlderThanSpecification : CompositeSpecification<Person>
{
private string name;
private int age;
public PersonNamedOlderThanSpecification(string name, int age)
{
this.name = name;
this.age = age;
}
public override bool IsSatisfiedBy(Person entity)
{
return (entity.Name.Contains(this.name)) && (entity.Age > age);
}
}
那么你可以这样使用它:
var personSpecs = new PersonNamedOlderThanSpecification("So", 28);
var personSpecs2 = new PersonNamedOlderThanSpecification("Some", 36);
var filtered = people.FindAll(x => personSpecs.IsSatisfiedBy(x));
var adjusted = people.FindAll(x => personSpecs2.IsSatisfiedBy(x));
你可以试试这个:
var people = new List<Person>
{
new Person {FullName = "Some Dude", Age = 45},
new Person {FullName = "Another Dude", Age = 28},
new Person {FullName = "Some Other Dude", Age = 36}
};
var filtered = people.Where(person => person.Age > 28 && person.FullName.StartsWith("So"));
var narrowlyFiltered = people.Where(person => person.Age > 36 && person.FullName.StartsWith("Some"));
var intersection = filtered.Intersect(narrowlyFiltered);
if (intersection != null)
{
if (intersection.Count() > 0)
{
//narrowlyFiltered is subset of filtered
}
}
这完全取决于你如何权衡相等的东西,比较表达式时什么更重要等等。
例如,如果您有完全不同的过滤器,那么在实际执行之前您可能不会知道查询差异。
要完全控制您的比较,请创建一个过滤器 class,其中包含一些可用于过滤的属性,然后构建表达式并使用此 class 进行比较,而不是使用访问者。
您可以准备用于比较整数、整数对(用于范围)等的通用函数。
我没有检查下面的代码,但它应该是一个好的开始。
public class PersonFilter: IComparable<PersonFilter>
{
public int? MinAge { get; set; }
public int? MaxAge { get; set; }
public string NamePrefix { get; set; }
public Expression<Predicate<Person>> Filter
{
return people => people.Where(person => (!MinAge.HasValue || person.Age > MinAge.Value) &&
(!MaxAge.HasValue || person.Age < MaxAge.Value) &&
(string.IsNullOrEmpty(NamePrefix) || person.FullName.StartsWith(NamePrefix))
}
// -1 if this filter is filtering more than the other
public int CompareTo(PersonFilter other)
{
var balance = 0; // equal
if(MinAge.HasValue != other.MinAge.HasValue)
{
balance += MinAge.HasValue ? -1 : 1;
}
else if(MinAge.HasValue)
{
balance += MinAge.Value.CompareTo(other.MinAge.Value) ?
}
if(string.IsNullOrEmpty(NamePrefix) != string.IsNullOrEmpty(other.NamePrefix))
{
balance += string.IsNullOrEmpty(NamePrefix) ? -1 : 1;
}
else if(!string.IsNullOrEmpty(NamePrefix))
{
if(NamePrefix.StartsWith(other.NamePrefix))
{
balance -= 1;
}
else if(other.NamePrefix.StartsWith(NamePrefix))
{
balance += 1;
}
else
{
// if NamePrefix is the same or completely different let's assume both filters are equal
}
}
return balance;
}
public bool IsSubsetOf(PersonFilter other)
{
if(MinAge.HasValue != other.MinAge.HasValue)
{
if(other.MinAge.HasValue)
{
return false;
}
}
else if(MinAge.HasValue && MinAge.Value < other.MinAge.Value)
{
return false;
}
if(string.IsNullOrEmpty(NamePrefix) != string.IsNullOrEmpty(other.NamePrefix))
{
if(!string.IsNullOrEmpty(other.NamePrefix))
{
return false;
}
}
else if(!string.IsNullOrEmpty(NamePrefix))
{
if(!NamePrefix.StartsWith(other.NamePrefix))
{
return false;
}
}
return true;
}
}
假设我在集合中有以下表达式:
var people = new List<Person>
{
new Person {FullName = "Some Dude", Age = 45},
new Person {FullName = "Another Dude", Age = 28},
new Person {FullName = "Some Other Dude", Age = 36}
};
var filtered = people.Where(person => person.Age > 28 && person.FullName.StartsWith("So"));
var narrowlyFiltered = people.Where(person => person.Age > 36 && person.FullName.StartsWith("Some"));
有没有办法在运行时比较这两个表达式并推断出第二个表达式是第一个表达式的子集?无需枚举或其他任何内容。我只有表达式,我正在尝试找出这些表达式是否相交或包含彼此。
如果您可以枚举您的集合,您可以先将元素放在 HashSet<T>
中,然后 运行 将 HashSet<T>.IsSubSet
放在上面:
HashSet<T> hs = new HashSet<T>(filtered);
HashSet<T> hs2 = new HashSet<T>(narrowlyFiltered);
hs.IsSubSetOf(hs2); //<- booleans saying true or false
否则,这个问题一般undecidable problem。尽管有启发式方法可以适用于许多 情况。例如,您可以尝试使用旨在在编译时推断出这一点的代码契约。
证明:
The formal variant is: given two Turing machines (methods, delegates, pointers), does every string contained in the first language is contained in the second?
Undecidable
proof: given it was decidable, EQTM would be decidable: simply first validate whether the first Turing machine is a subset of the second and vice versa. If both are subsets, we know they accept the same set of strings.
换句话说,如果你能做到这一点,你也可以推断出两个函数是否产生相同的结果,which cannot be implemented。
您必须将每个 Expression 分解为所有可能的继承类型(MethodCallExpression、ConditionalExpression 等),然后并行进行每个分解并检查每个可能的参数...编码会有点长。 .. 你可以从 ExpressionEqualityComparer
中得到启发看看Specification design pattern
一旦实施,您的规范在这种情况下就变成了
public class PersonNamedOlderThanSpecification : CompositeSpecification<Person>
{
private string name;
private int age;
public PersonNamedOlderThanSpecification(string name, int age)
{
this.name = name;
this.age = age;
}
public override bool IsSatisfiedBy(Person entity)
{
return (entity.Name.Contains(this.name)) && (entity.Age > age);
}
}
那么你可以这样使用它:
var personSpecs = new PersonNamedOlderThanSpecification("So", 28);
var personSpecs2 = new PersonNamedOlderThanSpecification("Some", 36);
var filtered = people.FindAll(x => personSpecs.IsSatisfiedBy(x));
var adjusted = people.FindAll(x => personSpecs2.IsSatisfiedBy(x));
你可以试试这个:
var people = new List<Person>
{
new Person {FullName = "Some Dude", Age = 45},
new Person {FullName = "Another Dude", Age = 28},
new Person {FullName = "Some Other Dude", Age = 36}
};
var filtered = people.Where(person => person.Age > 28 && person.FullName.StartsWith("So"));
var narrowlyFiltered = people.Where(person => person.Age > 36 && person.FullName.StartsWith("Some"));
var intersection = filtered.Intersect(narrowlyFiltered);
if (intersection != null)
{
if (intersection.Count() > 0)
{
//narrowlyFiltered is subset of filtered
}
}
这完全取决于你如何权衡相等的东西,比较表达式时什么更重要等等。 例如,如果您有完全不同的过滤器,那么在实际执行之前您可能不会知道查询差异。
要完全控制您的比较,请创建一个过滤器 class,其中包含一些可用于过滤的属性,然后构建表达式并使用此 class 进行比较,而不是使用访问者。 您可以准备用于比较整数、整数对(用于范围)等的通用函数。
我没有检查下面的代码,但它应该是一个好的开始。
public class PersonFilter: IComparable<PersonFilter>
{
public int? MinAge { get; set; }
public int? MaxAge { get; set; }
public string NamePrefix { get; set; }
public Expression<Predicate<Person>> Filter
{
return people => people.Where(person => (!MinAge.HasValue || person.Age > MinAge.Value) &&
(!MaxAge.HasValue || person.Age < MaxAge.Value) &&
(string.IsNullOrEmpty(NamePrefix) || person.FullName.StartsWith(NamePrefix))
}
// -1 if this filter is filtering more than the other
public int CompareTo(PersonFilter other)
{
var balance = 0; // equal
if(MinAge.HasValue != other.MinAge.HasValue)
{
balance += MinAge.HasValue ? -1 : 1;
}
else if(MinAge.HasValue)
{
balance += MinAge.Value.CompareTo(other.MinAge.Value) ?
}
if(string.IsNullOrEmpty(NamePrefix) != string.IsNullOrEmpty(other.NamePrefix))
{
balance += string.IsNullOrEmpty(NamePrefix) ? -1 : 1;
}
else if(!string.IsNullOrEmpty(NamePrefix))
{
if(NamePrefix.StartsWith(other.NamePrefix))
{
balance -= 1;
}
else if(other.NamePrefix.StartsWith(NamePrefix))
{
balance += 1;
}
else
{
// if NamePrefix is the same or completely different let's assume both filters are equal
}
}
return balance;
}
public bool IsSubsetOf(PersonFilter other)
{
if(MinAge.HasValue != other.MinAge.HasValue)
{
if(other.MinAge.HasValue)
{
return false;
}
}
else if(MinAge.HasValue && MinAge.Value < other.MinAge.Value)
{
return false;
}
if(string.IsNullOrEmpty(NamePrefix) != string.IsNullOrEmpty(other.NamePrefix))
{
if(!string.IsNullOrEmpty(other.NamePrefix))
{
return false;
}
}
else if(!string.IsNullOrEmpty(NamePrefix))
{
if(!NamePrefix.StartsWith(other.NamePrefix))
{
return false;
}
}
return true;
}
}