简体   繁体   English

当主键列返回 null 时,如何使用 select 运行 linq 查询,因此相关属性为 Z37A62786CC0C1DAE0BDBD94

[英]How to run linq query with select when a primary key column returns null and thus the related attribute is null

I have the following query:我有以下查询:

    var ComplaintsQuery = _context.TblComplaints.Select(p => new
    {
        ComplaintId = p.ComplaintId,
        ClientId = p.ClientId,
        ClientFullName = p.Client.ClientFullName,
        Datecomplaintreceived = p.Datecomplaintreceived,
        StaffmemberId = p.StaffmemberId,
        StaffFullName = p.Staffmember.StaffFullName,
    }
    )?.AsQueryable();

The database table for complaints is such that in some rows, clientId is null, thus I am am unable to retrieve ClientFullName , similarly for StaffFullname and StaffmemberId .投诉的数据库表是这样的,在某些行中, clientId是 null,因此我无法检索ClientFullName ,与StaffFullnameStaffmemberId类似。

Is there any way around this to have the query return null rather than a null exception which breaks the code?有什么办法可以让查询返回 null 而不是破坏代码的 null 异常?

Thanks to Jon Skeet's comment, the following code is functional with no null exception outputs.感谢 Jon Skeet 的评论,以下代码可以正常工作,没有 null 异常输出。

        var ComplaintsQuery = _context.TblComplaints.Select(p => new
        {
            ComplaintId = p.ComplaintId,
            ClientId = p.ClientId,
            ClientFullName = p.ClientId == null ? null : p.Client.ClientFullName,
            Datecomplaintreceived = p.Datecomplaintreceived,
            StaffmemberId = p.StaffmemberId,
            StaffFullName = p.StaffmemberId == null ? null : p.Staffmember.StaffFullName,
        }
        )?.AsQueryable();

Try尝试

var ComplaintsQuery = (from p in _context.TblComplaints
                               select new
                               {
                                   ComplaintId = p.ComplaintId,
                                   ClientId = p.ClientId,
                                   ClientFullName = p.ClientId == null ? null : p.Client.ClientFullName,
                                   Datecomplaintreceived = p.Datecomplaintreceived,
                                   StaffmemberId = p.StaffmemberId,
                                   StaffFullName = p.Staffmember == null ? null : p.Staffmember.StaffFullName,
                               }).AsQueryable();

Well if you use EntityFramework Core it won't give you exception on null but if you are using EntityFramework you should use ?好吧,如果您使用 EntityFramework Core,它不会在null上给您例外,但如果您使用的是 EntityFramework,您应该使用? after your expected null object like Client or StaffMember rather than the whole query在您预期的null object 之后像ClientStaffMember而不是整个查询

var ComplaintsQuery = _context.TblComplaints.Select(p => new
    {
        ComplaintId = p.ComplaintId,
        ClientId = p.ClientId,
        ClientFullName = p.Client?.ClientFullName,
        Datecomplaintreceived = p.Datecomplaintreceived,
        StaffmemberId = p.StaffmemberId,
        StaffFullName = p.Staffmember?.StaffFullName,
    }
    ).AsQueryable();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM