Context
- Three classes:
MetaParticipant
,MetaMovie
andMetaPerson
- A
MetaParticipant
has oneMetaMovie
and oneMetaPerson
To fix an issue, I created a IsEqual
static method in all three.
For the independent ones MetaMovie
and MetaPerson
, I used (MetaPerson
has the same except with its class instead):
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;
}
So, I would like to write the MetaParticipant.IsEqual
method, but ain't able to figure out how.
This method will receive a MetaParticipant
that can use its MetaMovie
and MetaPerson
to call the others.
Issue
Here is the MetaParticipant.Equals
that IsEqual
shall "replace":
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;
}
And where I am up to for 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;
}
Sorry, there is a bit of garbage I kept so you can see some tries I did.