select multiple column from include table in LINQ

c# entity-framework-6 linq sql-server

Question

My question is about how can i select some one-two column from those tables are included and at the end when i am selecting as list it is returning list of parent object but child are contain those column i mention to select?

 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 });

At the end I need to have list of home model (List) and just images and Address and region name have value and important just those are selected from database not all infromation in the tables. I am trying to make a query with better performance

Edit Add Models

 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; }
        ...
    }

absolutely tables have more column just add here as example

1
0
1/12/2017 10:55:16 AM

Popular Answer

you have to define a new type with the fields you need, or use an anonymous type.

.Select(x => new { 
  Images = x.Images,
  Address = x.Address,
  Indexs.Cities.Proviences.Regions =           
    x.Indexs.Cities.Proviences.Regions.Name });
0
1/12/2017 10:44:16 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow