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

时间:2015-10-23 16:50:23

标签: 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; }
    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属性为null时,以下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属性并且它返回了它应该返回的查询..我该怎么做才能解决这个问题?条件?:不起作用。

0 个答案:

没有答案