实体框架6 - 使用select的嵌套查询中的空值

时间:2016-11-10 08:26:51

标签: asp.net sql-server entity-framework asp.net-mvc-5 entity-framework-6

我在EF6遇到问题。当我执行查询Select时,它返回值。但是当我添加Select时,它返回null。

代码在这里:

这里的(W)不是空的......

        var list = db.X.Include("Y").Include("Z.W")
            .OrderBy(c => c.Id)
            .Skip(pageSize * page)
            .Take(pageSize)
            .ToList();

这里,W值为空......

        var list = db.X.Include("Y").Include("Z.W")
            .Select(a => new { a.Id, a.Z})
            .OrderBy(c => c.Id)
            .Skip(pageSize * page)
            .Take(pageSize)
            .ToList();

请帮助:)

更新1

public class academy
{
    public int Id { get; set; }
   [StringLength(255)]
    [Index(IsUnique = true)]
    public string Name { get; set; }
    public string Logo { get; set; }
    [Required]
    public  Owner owner { get; set; }

    public  List<location> Location { get; set; }           
}


public class location
{
    public int Id { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    public string City { get; set; }
    public string Region { get; set; }
    public string Neighborhood { get; set; }
    public string Street { get; set; }

    public  academy Academy { get; set; }

    public  List<stadium> Stadiums { get; set; }

    public List<Administrators> Administrators { get; set; }

    public List<addition> Addition { get; set; }

    public List<Pricing> Pricing { get; set; }

    public List<time_frame> TimeFrames { get; set; }

    [NotMapped]
    public string Details {
        get { return (City + " - " + Street); }
    }

}


public class Pricing
{

    public int Id { get; set; }
    public double Price { get; set; }
    public double? PriceAfterOffer { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }

    public location Location { get; set; }
    public players_capacity StadiumCapacity { get; set; }


}

public class players_capacity
{
    public int Id { get; set; }
    [StringLength(255)]
    [Index(IsUnique = true)]
    public string Capacity { get; set; }

}


        var list = db.locations
            .Select(a => new { a.Id, a.City, a.Region, a.Street, a.Latitude, a.Longitude, a.Pricing, a.Academy })
            .OrderBy(c => c.Id)
            .Skip(pageSize * page)
            .Take(pageSize)
            .ToList();

问题在于players_capacity始终为null

1 个答案:

答案 0 :(得分:1)

Include指定的任何其他数据如果查询更改&#34;形状&#34;则会被忽略,在这种情况下,您的其他.Select表达式会使先前的Include条款无效,因此它们会被忽略被忽略了。如果您执行GroupByGroupJoin,则会发生相同的情况。

幸运的是,修复很简单:在投影中明确指定YZ.W成员:

var list = db.X
    .Select( x => new { x.Id, x.Z, x.Y, x.Z.W } )
    .OrderBy( p => p.Id )
    .Skip( pageSize * page )
    .Take( pageSize )
    .ToList();