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 的信息,并且我遇到了实用程序的 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)在链接之前添加一个.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 回答