linq 如何获取列表中为空的属性的名称沿着列表中应始终存在的固定属性

nr9pn0ug  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(127)

我试图获得一个列表,其中包含值等于null的属性的名称沿着一个应该始终存在的属性(InvoiceNumber)。

public class InvoiceDetails
{
     public string InvoiceNumber { get; set; }
     public string InvoiceClass { get; set; }
     public string ExpenseMethod { get; set; }
     public string FromDate { get; set; }
     public string ToDate { get; set; }
}

我有两个代码集,(#1)-这只返回InvoiceNumber在代码中提到的其他属性值等于空,这是属性应该总是在列表中。这(#2)返回的属性值等于空。但我想以某种方式返回的清单与InvoiceNumber的属性名称是空的。

(#1) var invoice= InvoiceDetails.Where(x => x.FromDate == null || x.ExpenseMethod == null || x.ToDate == null).Select(s => s.InvoiceNumber ).Distinct().ToList();

(#2) var result = typeof(InvoiceDetails).GetProperties()
        .Select(x => new { property = x.Name, value = x.GetValue(objInvoiceDetails) })
        .Where(x => x.value != null)
        .ToList();

例如g 101|起始日期|费用方法|
此处发票编号101的属性FromDate和Expense方法具有空值

hc8w905p

hc8w905p1#

I'm trying to get a list which contains the name of the properties which have value equals to null along with a property(InvoiceNumber)

听起来像是要返回Dictionary<string, List<string>>

var dictionary = new Dictionary<string, List<string>>();
var nullInvoices = InvoiceDetails.
    Where(x => x.FromDate == null || x.ExpenseMethod == null || x.ToDate == null).
    Distinct();
foreach (var invoiceDetails in nullInvoices )
{
    var key = invoiceDetails.InvoiceNumber;
    var val = typeof(InvoiceDetails).GetProperties().
        Where(x => x.GetValue(invoiceDetails) == null).
        Select(x => x.Name).
        ToList();
    dictionary[key] = val;
}

相关问题