首页 > 解决方案 > 无法从列中检索具有空值的数据

问题描述

我有如下数据。

AccountNo,  PaymentCode, Priority
NULL,       01,          99
NULL,       01,          99
0711712916, 03,          99
0711712916, 03,          99

我想获取上面的所有数据,我尝试使用下面的代码

 var prioritasPembayarans = db.VWPrioritasPembayarans.Where(x => arrValidPaymentCodes.Contains(x.PaymentCode))
                                                     .OrderBy(x => x.AccountNo)
                                                     .ThenBy(x => x.Priority)
                                                     .ToList();

班级

public class VWPrioritasPembayaran
{
    [Key, Column(Order = 0)]
    public string AccountNo { get; set; }
    [Key, Column(Order = 1)]
    public string PaymentCode { get; set; }
    public byte Priority { get; set; }
    public decimal EndingBalance { get; set; }
    public decimal TotalTransfer { get; set; }
}

当我调试时,我得到 4 个数据, 这是屏幕截图. 在第三个和第四个数据中,我可以展开并查看数据,但在第一个和第二个数据中,我得到一个空值。我想问的是如何获取第三个和第四个数据之类的数据,即使AccountNo 列为空。谢谢

标签: c#asp.net-mvc

解决方案


一个技巧可以是 -

var prioritasPembayarans = db.VWPrioritasPembayarans.Where(x => arrValidPaymentCodes.Contains(x.PaymentCode))
                                                 .OrderBy(x => x.AccountNo != null ? x.AccountNo : "")
                                                 .ThenBy(x => x.Priority)
                                                 .ToList();

推荐阅读