C# 将DTO属性设置为空值不会产生任何结果

C# 将DTO属性设置为空值不会产生任何结果,c#,entity-framework,linq,linq-to-entities,entity-framework-core,C#,Entity Framework,Linq,Linq To Entities,Entity Framework Core,给定以下DTO类: public class DistrictDetailDTO { public DistrictDetailDTO() { } public int Id { get; set; } public string CountyFP { get; set; } public long? EstStudentPop { get; set; } public string Name { get; set; } pub

给定以下DTO类:

public class DistrictDetailDTO
{
    public DistrictDetailDTO()
    {

    }

    public int Id { get; set; }
    public string CountyFP { get; set; }
    public long? EstStudentPop { get; set; }
    public string Name { get; set; }
    public short? Ranking { get; set; }
    public string RANumber { get; set; }
    public long? SchoolCount { get; set; }        
    public string Coop { get; set; }
    public string County { get; set; }
    public virtual DirectorDTO Director { get; set; }
    public virtual AddressDTO Address { get; set; }
}
当地区的Coop属性为空时,以下LINQ查询无法返回任何结果

var district = from districts in _ctx.District
                        where districts.Id == id
                        select new DistrictDetailDTO
                        {
                            Id = districts.Id,
                            CountyFP = districts.County.FIPSCode,
                            EstStudentPop = districts.EstStudentPop,
                            Name = districts.Name,
                            Ranking = districts.Ranking,
                            RANumber = districts.RANumber,
                            SchoolCount = districts.SchoolCount,
                            Address = new AddressDTO {
                                Line1 = districts.Address.Line1,
                                Line2 = districts.Address.Line2,
                                City = districts.Address.City,
                                State = districts.Address.State,
                                Zipcode = districts.Address.Zipcode
                            },
                            Coop = (districts.Coop==null) ? "None" : districts.Coop.Name,
                            County = districts.County.Name,
                            Director = new DirectorDTO {
                                Email = districts.Director.Email,
                                ImgUrl = districts.Director.ImgUrl,
                                Name = districts.Director.Name,
                                Phone = districts.Director.Phone
                            }
                        };

如果我从DTO和查询中删除Coop属性,它将按原样返回。。我能做些什么来解决这个问题?条件?:不起作用。

您是否尝试查看EF生成的SQL?我打赌,您会在其中找到更多细节。请显示
地区
合作社
之间关联的映射。从您的代码中,
地区。Id==Id
结果取决于Id,而不是合作社。由于其他原因,您确定查询没有返回结果吗?Coop是地区实体/模型的导航属性。。。。从District实体类:[ForeignKey(“CoopId”)][InverseProperty(“District”)]public-virtual-Coop-Coop{get;set;}--我还应该注意,并非所有的District都有一个与之关联的Coop,在这种情况下,CoopId(int?)和Coop本身应该为null。您实际使用的是EF7 beta吗?