【发布时间】: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