2017-01-12 9 views
0

私の質問は、これらのテーブルからいくつかの1-2カラムを選択する方法についてです。リストとして選択すると、親オブジェクトのリストが返されますが、私はそれらの列を選択するために言及?終わり複数の列をLINQのインクルードテーブルから選択する

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

私は家モデル(リスト)のリストを持っている必要があり、ちょうど画像とアドレスと地域の名前は価値と重要なものだけではデータベースではありません、テーブル内のすべてのinfromationから選択されています。私はより良いパフォーマンスでクエリを作成しようとしています

編集を

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

絶対にテーブルは複数の列は一例

+0

インデックス、都市、居住地および地域。それらはコレクションの名前のように聞こえる。あなたもここにあなたのモデルを投稿していただけますか? – sachin

+0

exampleモデルが追加されました – Hesam

答えて

0

として、ここで追加したモデルを追加し、あなたが必要な分野で新しい型を定義する必要があります、または匿名型を使用します。

.Select(x => new { 
    Images = x.Images, 
    Address = x.Address, 
    Indexs.Cities.Proviences.Regions =   
    x.Indexs.Cities.Proviences.Regions.Name }); 
+0

はい、それは匿名タイプですが、私のコードでタイプを認識できませんでした – Hesam

+0

新しいタイプを定義する必要があります! –

+0

私はEFが.Select(x =>新しいホーム{ 画像の=のx.Images、 アドレス= x.Address、 Indexs.Cities.Proviences.Regions.Name = x.Indexsのようないくつかのことを定義することはできません知っているように。市町村。 – Hesam

関連する問題