具有静态实例成员的实体查询条件

时间:2016-05-05 13:52:05

标签: c# entity-framework criteria

我正在尝试创建一个类似订单状态历史记录表的审计跟踪。这样,Orders可以有许多OrderStates,以及指向最新历史项的单个State。保存更新时到目前为止一直很好。当我尝试查询时,就会出现问题,就好像我在使用枚举一样:

public class OrderState
{
    public static OrderState Placed = new OrderState("Placed", 1, 1);
    public static OrderState Accepted = new OrderState("Accepted", 10, 2);
    public static OrderState Cancelled = new OrderState("Cancelled", 20, 3);
    public static OrderState Completed = new OrderState("Completed", 30, 4);

    protected OrderState()
    {
    }

    public OrderState(string name, int order, int id)
    {
        Name = name;
        Order = order;
        Id = id;
    }

    public int Id { get; set; }
    public string Name { get; protected set; }
    public int Order { get; protected set; }



    public static bool operator == (OrderState state1, OrderState state2)
    {
        if (ReferenceEquals(state1, null))
        {
            return ReferenceEquals(state2, null);
        }
        return state1.Equals(state2);
    }

    public static bool operator !=(OrderState state1, OrderState state2)
    {
        return !(state1 == state2);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
        {
            return false;
        }
        if (ReferenceEquals(this, obj))
        {
            return true;
        }
        if (!(obj is OrderState))
        {
            return false;
        }
        return Equals((OrderState)obj);
    }

    public virtual bool Equals(OrderState other)
    {
        return other.Id.Equals(Id);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            return ((Id.GetHashCode())*397) ^ Order;
        }
    }


}

订单类

public class Order
{
    public Order()
    {
        Progress(OrderState.Placed);
    }
    public int Id { get; set; }

    public virtual OrderState State
    {
        get { return States.OrderByDescending(x => x.State.Order).FirstOrDefault()?.State; }
    }

    public void Progress(OrderState state)
    {
        if (States.All(x => x.State != state))
        {
            States.Add(new OrderStateHistory()
            {
                Order = this,
                State = state
            });
        }
    }

    public virtual ICollection<OrderStateHistory> States { get; set; } = new List<OrderStateHistory>();
}

在我的代码中,这些工作正常: order.Progress(OrderState.Accepted);if (order.State == OrderState.Accepted)

但是,我想要访问的是Where(x => x.State.Equals(OrderState.Accepted))Where(x => x.State == OrderState.Accepted)

不幸的是,任何一个标准都会产生一个&#39;指定的类型成员&#39;状态&#39; LINQ to Entities不支持。仅支持初始值设定项,实体成员和实体导航属性。&#39;错误。

我知道我已经能够用NHibernate做到这一点。我甚至可以用EF吗?

1 个答案:

答案 0 :(得分:0)

  

由于EF需要将LINQ语句转换为SQL语句,   你不能这样做。如果你有复杂的比较逻辑   你必须在overridden Equals()方法中复制它   LINQ声明。

public IQueryable<Foo> FoosEqualTo(IQueryable<Foo> allFoos, Foo target) {
return from foo in allFoos
       where foo.Id == target.Id // or other comparison logic...
       select foo;
}

public Foo getFoo(Foo target) {
    return FoosEqualTo(DC.foos, target).FirstOrDefault();
}