0

私のソリューションには次のエンティティがあります

public class UtilityAccount : IObjectWithState 
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid UtilityAccountID { get; set; }
    public string Account { get; set; }
    [ForeignKey("Person")]
    public Guid PersonID { get; set; }
    public virtual Person Person { get; set; }
    public string ForeignID { get; set; }
    [NotMapped]
    public ObjectState ObjectState { get; set; }

    public virtual ICollection<Utility> Utilities { get; set; }

    public UtilityAccount()
    {
        Utilities = new List<Utility>();
    }
}

public class Utility : IObjectWithState 
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid UtilityID { get; set; }
    [ForeignKey("UtilityAccount")]
    public Guid UtilityAccountID { get; set; }
    public virtual UtilityAccount UtilityAccount { get; set; }
    public Guid? ServiceAddressID { get; set; }
    [ForeignKey("ServiceAddressID")]
    public virtual Address ServiceAddress { get; set; }
    [NotMapped]
    public ObjectState ObjectState { get; set; }
    public double CurrentBalance { get; set; }
    public double? PendingPaymentTotal { get; set; }
    public string ForeignID { get; set; }
    [ForeignKey("UtilityType")]
    public Guid UtilityTypeID { get; set; }
    public virtual UtilityType UtilityType { get; set; }
    public virtual ICollection<UtilityBill> UtilityBills { get; set; }
    public virtual ICollection<IncomingUtilityPayment> IncomingPayments { get; set; }

    public Utility()
    {
        UtilityBills = new List<UtilityBill>();
        IncomingPayments = new List<IncomingUtilityPayment>();
    }
}

public class IncomingUtilityPayment : IObjectWithState
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid IncomingPaymentID { get; set; }
    public string ForeignID { get; set; }
    [ForeignKey("Utility")]
    public Guid UtilityID { get; set; }
    public virtual Utility Utility { get; set; }
    public DateTime PaymentDate { get; set; } 

    public IncomingPaymentStatus IncomingPaymentStatus { get; set; }
    public double? UtilityAmount { get; set; }
    public double? ConvenienceFee { get; set; }
    public double? TotalAmount { get; set; }

    public string AuthCode { get; set; }
    public string AuthReference { get; set; }
    public string TenderType { get; set; }
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int PaymentIdent { get; set; }

    [NotMapped]
    public ObjectState ObjectState { get; set; }
}

私の問題は、Linq を使用して UtilityAccount に関する情報を取得しようとしていて、Utility の IncomingPayments で問題が発生していることです。以下は、私が使用しようとしている選択ステートメントです。

returnVal = repo.AllIncluding(o => o.Person, o => o.Utilities, o => o.Utilities.Select(p => p.UtilityType), o => o.Person.BillingAddress, o => o.Utilities.Select(p => p.ServiceAddress), o => o.Utilities.Select(p => p.IncomingPayments.Where(q => q.IncomingPaymentStatus == IncomingPaymentStatus.Pending || q.IncomingPaymentStatus == IncomingPaymentStatus.Processed )));

この句をステートメントに追加するまで、すべてがうまくいきました。

o => o.Utilities.Select(p => p.IncomingPayments.Where(q => q.IncomingPaymentStatus == IncomingPaymentStatus.Pending || q.IncomingPaymentStatus == IncomingPaymentStatus.Processed ))

私の問題は、Linq 句で間違って書いているものになると思います。私が得ているエラーは

The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties.

パラメータ名: パス

次のステートメントを問題なく使用できます

o => o.Utilities.Select(p => p.IncomingPayments)

where句を追加するとすぐにエラーが発生します

4

1 に答える 1

0

私は EntityFramework にも linq-to-entities にも精通していませんが、linq-to-object のようなものであれば、次のことができます。

このよう.Where(p => p.IncomingPayments != null)に連鎖する前に a を追加します.Select()

o.Utilities.Where(p => p.IncomingPayments != null)
           .Select(p => p.IncomingPayments.Where(q => q.IncomingPaymentStatus == IncomingPaymentStatus.Pending || q.IncomingPaymentStatus == IncomingPaymentStatus.Processed))

結果はネストされた IEnumerable になります。IEnumerable<IEnumerable<IncomingUtilityPayment>>

本当に必要な場合はIEnumerable<IncomingUtilityPayment>.SelectMany()遊びに来てください。

o.Utilities.Where(p => p.IncomingPayments != null)
           .SelectMany(p => p.IncomingPayments)
           .Where(q => q.IncomingPaymentStatus == IncomingPaymentStatus.Pending || q.IncomingPaymentStatus == IncomingPaymentStatus.Processed)

この助けを願っています

于 2013-04-24T16:24:26.700 に答える