嵌套列表属性上的 C# Linq Where 子句

C# Linq Where Clause on Nested List Attribute

我有一个 class Proforma,其中包含一个 MonthlyPayment 类型的列表。这是代码:

public class Proforma 
{
    List<MonthlyPayment> lstMonthlyPayment = new List<MonthlyPayment>();
    // Other Attributes
}

List<Proforma> lstProforma = Fetch();

现在我想 select 来自 lstProforma 的那些记录,其中 lstMonthlyPayment.Month==DateTime.Now.Month

我怎样才能做到这一点?

您可以使用 linq 查询,例如:

 var query = from proforma in lstProforma
             from payment in proforma.lstMonthlyPayment
             where payment.Month == DateTime.Now.Month
             select proforma;

正如 Tim 在他的 中指出的那样,您不应为月份使用字符串,因为您的代码可能容易出现问题和拼写错误。您应该使用 DateTime 属性(Date 例如在 class MonthlyPayment 中)或整数。