You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写MetaParticipant.IsEqual表达式方法,组合其他类判断逻辑?

上下文

  • 三个类:MetaParticipant、MetaMovie和MetaPerson
  • 一个MetaParticipant包含一个MetaMovie和一个MetaPerson

为解决问题,我在三个类中都创建了IsEqual静态方法。

对于独立的MetaMovie和MetaPerson类,我使用了如下实现(MetaPerson的实现类似,仅类名不同):

public static System.Linq.Expressions.Expression<Func<MetaMovie, bool>> IsEqual(MetaMovie other)
{
    if (other.Id > 0) return m => other.Id == m.Id; // 使用'> 0'以跳过变更跟踪器中的新对象,使用下一个标识符

    return m => other.MetaSource == m.MetaSource && other.ExternalId == m.ExternalId;
}

现在我想要编写MetaParticipant.IsEqual方法,但不知道如何实现。

该方法接收一个MetaParticipant对象,需要使用其MetaMovie和MetaPerson属性调用对应的IsEqual方法。

问题

以下是MetaParticipant.IsEqual需要替代的MetaParticipant.Equals方法:

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;
}

抱歉,我保留了一些尝试的代码片段,以便您了解我的思路。

内容的提问来源于stack exchange,提问作者Master DJon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 04:46:46