Context
- 三类:
MetaParticipant
, MetaMovie
and MetaPerson
- A
MetaParticipant
有一个MetaMovie
和一个MetaPerson
为了解决问题,我创建了一个IsEqual所有三个中的静态方法 https://stackoverflow.com/questions/75393985/c-sharp-using-equals-in-where-clause-doesnt-translate-to-proper-query.
对于独立的人来说MetaMovie
and MetaPerson
, 我用了 (MetaPerson
除了其类之外具有相同的特征):
public static System.Linq.Expressions.Expression<Func<MetaMovie, bool>> IsEqual(MetaMovie other)
{
if (other.Id > 0) return m => other.Id == m.Id; // Using '> 0' so it skips the new ones in change tracker to the next identifier
return m => other.MetaSource == m.MetaSource && other.ExternalId == m.ExternalId;
}
所以,我想写下MetaParticipant.IsEqual
方法,但无法弄清楚如何。
该方法将收到一个MetaParticipant
可以使用它的MetaMovie
and MetaPerson
给其他人打电话。
Issue
这里是MetaParticipant.Equals
that IsEqual
应“替换”:
public override bool Equals(object obj)
{
if (obj == null) return false;
if (base.Equals(obj)) return true;
if (obj is not MetaParticipant other) return false;
return Movie.Equals(other.Movie) && Person.Equals(other.Person) && JobTitle == other.JobTitle;
}
我要做的是什么IsEqual
:
public static Expression<Func<MetaParticipant, bool>> IsEqual(MetaParticipant other)
{
//var own = new Expression<Func<MetaParticipant, bool>() { return x => x.JobTitle == other.JobTitle; };
var mm = MetaMovie.IsEqual(other.Movie);
var mp = MetaPerson.IsEqual(other.Person);
var body = Expression.AndAlso(
Expression.Invoke(mm, Expression.Parameter(other.Movie.GetType(), "mm")),
Expression.Invoke(mp, Expression.Parameter(other.Person.GetType(), "mp"))
);
//body = Expression.AndAlso(body, );
var lambda = Expression.Lambda<Func<MetaParticipant, bool>>(body, Expression.Parameter(typeof(MetaParticipant)));
return lambda;
//return m => Expression.Invoke(mm, Expression.Variable(m.Movie.GetType())) && m.JobTitle == other.JobTitle;
}
抱歉,我保留了一些垃圾,所以你可以看到我所做的一些尝试。