【问题标题】:Joining two tables displays NULL [closed]连接两个表显示 NULL [关闭]
【发布时间】:2021-10-12 06:25:03
【问题描述】:

我想从表 usersDetail 中获取添加了 Firstname 和 Lastname 的 Invoice 表中的所有数据。

我的查询正在返回 Invoice 表中的数据,但 NULL 的值在 Firstname 和 Lastname 上。

你能帮忙吗?

表格字段


发票表 = InvoiceId, Date, CreateByUser(Guid), Vehicule

UserDetail 表 = UserId(string), FirstName, Lastname

CreateByUser = UserId

var invoiceContext = _context.Invoices.ToList();

var usersDetail = _userManager.Users.Select(x => new ApplicationUser { Id = x.Id, FirstName = x.FirstName, SurName = x.SurName }).ToList();



var result = from      invoice in invoiceContext
                       join
                       usrDtl in usersDetail
                       on invoice.CreateByUser.ToString() equals usrDtl.Id into tempstorage
                       from dx in tempstorage.DefaultIfEmpty()
                       select new InvoiceViewModel
                       {
                           Id = invoice.InvoiceId.ToString(),
                           Date = invoice.Date,
                           Vehicle = invoice.engine,
                           
                           FirstName = (dx != null) ? dx.FirstName : "NULL",
                           SurName = (dx != null) ? dx.SurName : "NULL"
                       };

类模型

public class Invoice
{
    public int InvoiceId { get; set; }

    public DateTime Date { get; set; }

    public string Vehicle { get; set; }

    public Guid CreateByUser { get; set; }

    public int TotalInvoice { get; set; }

    public IList<ProductInvoice> ProductInvoices { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }

    public string SurName { get; set; }
}



public class InvoiceViewModel
{
    public int InvoiceId { get; set; }

    public Guid CreateByUser { get; set; }

    public DateTime Date { get; set; }

    public int TotalInvoice { get; set; }

    public string FirstName { get; set; }

    public string SurName { get; set; }
    
    public string Vehicle { get; set; }

}

【问题讨论】:

  • dx != null 错误 -> 使用相同的 Dx.FirstNameIsNull()?
  • 您确定这与invoice.CreateByUser.ToString() equals usrDtl.Id 匹配吗?可能涉及前导或尾随空格。无论哪种方式,最好显示类模型,以便我们知道问题是关于什么的。
  • @GertArnold 我编辑了上面的帖子以添加类模型。
  • 那么比赛呢?
  • @GertArnold 非常抱歉,这是我自己的错误,从我的帖子中说 我的查询正在从 Invoice 表返回数据,但 Firstname 和 Lastname 的值为 NULL。。我在调试时得到了 NULL 值,因为它是一个包含 50 个数据的长列表,所以,当我调试时,我只遍历 10-15 个第一个数据来检查......所以我没有查看整个数据列表。跨度>

标签: c# .net asp.net-mvc linq lambda


【解决方案1】:

试试这个

var result = (from invoice in invoiceContext
            join usrDtl in usersDetail
             on invoice.CreateByUser.ToString() equals usrDtl.Id into usrDtlj 
             from usrDtl in usrDtlj.DefaultIfEmpty()
             select new InvoiceViewModel
              {
              Id = invoice.InvoiceId.ToString(),
              Date = invoice.Date,
              Vehicule = invoice.engine,
              FirstName = usrDtl.FirstName==null? "NULL":usrDtl.FirstName,
              SurName = usrDtl.SurName==null? "NULL":usrDtl.SurName
              }).ToList();

【讨论】:

  • 为什么添加ToList会改变什么?他们显然已经有了输出。无论如何,尝试某事的建议对 cme​​ts 有好处,而不是答案。
  • 我收到System.NullReferenceException 对象引用未设置为对象的实例。 完全在usrDtl
  • 我得到 System.NullReferenceException 对象引用未设置为对象的实例。完全从 usrDtlj.DefaultIfEmpty() 中的 usrDtl 开始
  • @Serge 它仅在您从选择中删除 InvoiceViewModel 并使其成为匿名类型时才有效。不知道为什么??我现在刚刚尝试过,并且在删除 InvoiceViewModel 后可以正常工作。那为什么会这样呢?
  • @Serge 非常抱歉,这是我自己的错误,从我的帖子中说 我的查询正在从 Invoice 表返回数据,但 Firstname 和 Lastname 的值为 NULL。。我在调试时得到了 NULL 值,因为它是一个包含 50 个数据的长列表,所以,当我调试时,我只遍历 10-15 个第一个数据来检查......所以我没有查看整个数据列表。跨度>
【解决方案2】:
var result = (from invoice in invoiceContext
         join usrDtl in usersDetail
         on invoice.CreateByUser.ToString() equals usrDtl.Id into usrDtlj 
         from usrDtl in usrDtlj.DefaultIfEmpty()
         select new InvoiceViewModel
          {
          Id = invoice.InvoiceId.ToString(),
          Date = invoice.Date,
          Vehicule = invoice.engine,
          FirstName = usrDtl==null?"":usrDtl.FirstName??"" ,
          SurName = usrDtl==null?"":usrDtl.SurName??""
          }).ToList();

试试这个。它将正常工作。

【讨论】:

    猜你喜欢
    • 2019-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 2017-07-26
    相关资源
    最近更新 更多