从LINQ中的include表中选择多个列

时间:2017-01-12 10:17:21

标签: c# sql-server linq entity-framework-6

我的问题是如何从这些表中选择一两列的列,最后当我选择列表时,它返回父对象的列表,但是子列表包含我提到的那些列?< / p>

 var testq = _db.Homes
   .Include(x => x.Indexs.Cities.Proviences.Regions)
   .Include(x => x.Images)
   .Select(x => new Homes { 
      Images = x.Images,
      Address = x.Address,
      Indexs.Cities.Proviences.Regions =           
        x.Indexs.Cities.Proviences.Regions.Name });

最后我需要有家庭模型列表(List),只有图像和地址和区域名称有价值和重要的只是那些从数据库中选择而不是表格中的所有信息。我正在尝试以更好的性能进行查询

修改添加模型

 public partial class dbContext : DbContext
    {
        public virtual DbSet<City> Cities { get; set; }
        public virtual DbSet<Province> Provinces { get; set; } 
        public virtual DbSet<Region> Regions { get; set; } 
        public virtual DbSet<Index> Indexs { get; set; } 
        public virtual DbSet<Home> Homes { get; set; } 
        public virtual DbSet<Images> Imageses { get; set; } 

    }
    public partial class Home
    {
        public Home()
        {
            Imageses = new HashSet<Images>();
        }

        [Key]
        public int IDHome { get; set; }
        [Required]
        [StringLength(5)]
        public string Cap { get; set; }
        [Required]
        [StringLength(10)]
        public string Number { get; set; }
        [Required]
        [StringLength(50)]
        public string Address { get; set; }
        ....
        public virtual Index Indexs { get; set; }
        public virtual ICollection<Images> Imageses { get; set; }
    }
    public class Index
    {
        public int ID { get; set; }
        public int IDHome { get; set; }
        ....
        public virtual City Cities { get; set; }
    }
    public partial class City
    {
        [Key]
        public int ID { get; set; }
        public int IDProvincia { get; set; }
        public decimal Latitudine { get; set; }
        public decimal Longitudine { get; set; }
        [Required]
        [StringLength(100)]
        public string Name { get; set; }
        ....
        public virtual Province Provinces { get; set; }
    }
   public partial class Province
    {
        [Key]
        public int ID { get; set; }
        public int IDRegione { get; set; }
        [Required]
        [StringLength(50)]
        public string Name { get; set; }
        [Required]
        [StringLength(3)]
        public string Init { get; set; }
        ...
        public virtual Region Regions { get; set; }
    }
     public partial class Region
    {
        [Key]
        public int ID { get; set; }
        [Required]
        [StringLength(50)]
        public string Name { get; set; }
        public DateTime DataInsert { get; set; }
        ...
    }

   public class Images
    {
        public int ID { get; set; }
        public string Path { get; set; }
        ...
    }

绝对表有更多列只是在这里添加为例子

1 个答案:

答案 0 :(得分:0)

您必须使用所需的字段定义新类型,或使用匿名类型。

.Select(x => new { 
  Images = x.Images,
  Address = x.Address,
  Indexs.Cities.Proviences.Regions =           
    x.Indexs.Cities.Proviences.Regions.Name });
相关问题