2016-09-11 14 views
0

特定の子の親の方言( 'lu_dialect_t'から)の '名前'を表示しようとしています。私はLINQクエリで複数の左結合をやっていますが、今は 'parent_id'でクエリをGROUP化し、(親によって話された方言の) '名前'を1つの列に連結して格納する方法を見つけることを望んでいます私のViewModelの変数LINQでの複数の左結合、グループ化、および連結

これは私のViewModelです:

public class ParentViewModel 
{ 
    public int parent_id { get; set; } 
    public string last_name { get; set; } 
    public string first_name { get; set; } 
    public string middle_name { get; set; } 
    public string ext_name { get; set; } 
    public Nullable<System.DateTime> birthdate { get; set; } 
    public string civil_status { get; set; } 
    public string email_address { get; set; } 
    public string cell_num { get; set; } 
    public string tel_num { get; set; } 
    public string fax_num { get; set; } 
    public string room_num_or_building { get; set; } 
    public string street { get; set; } 
    public string purok { get; set; } 
    public string subdivision { get; set; } 
    public Nullable<int> brgy_id { get; set; } 
    public string city_code { get; set; } 
    public string province_code { get; set; } 
    public string mother_tongue { get; set; } 
    public string educational_attainment { get; set; } 
    public string occupational_status { get; set; } 
    public string parent_type { get; set; } 
    public string deceased { get; set; } 
    public Nullable<System.DateTime> survey_date_conducted { get; set; } 
    public string person_who_conducted { get; set; } 
    public int child_id { get; set; } 
    public string parent_dialects { get; set; } 
} 

は、これは私のコントローラである:

 public ActionResult Parents(int id) 
    { var query = (from p in db.parent_t 

     join cp in db.tn_child_parent_t on p.parent_id equals cp.parent_id into tcpGroup 
     from x in tcpGroup.DefaultIfEmpty() 

     join c in db.child_t on x.child_id equals c.child_id into cGroup 
     from y in cGroup.DefaultIfEmpty() 

     join pd in db.tn_parent_dialect_t on p.parent_id equals pd.parent_id into tpdGroup 
     from a in tpdGroup.DefaultIfEmpty() 

     join d in db.lu_dialect_t on a.dialect_id equals d.dialect_id into dGroup 
     from b in dGroup.DefaultIfEmpty() 

     where (y.child_id == id) 

     select new ViewModels.ParentViewModel 
     { 
      parent_id = p.parent_id, 
      last_name = p.last_name, 
      first_name = p.first_name, 
      middle_name = p.middle_name, 
      ext_name = p.ext_name, 
      birthdate = p.birthdate, 
      civil_status = p.civil_status, 
      email_address = p.email_address, 
      cell_num = p.cell_num, 
      tel_num = p.tel_num, 
      fax_num = p.fax_num, 
      room_num_or_building = p.room_num_or_building, 
      street = p.street, 
      purok = p.purok, 
      subdivision = p.subdivision, 
      brgy_id = p.brgy_id, 
      city_code = p.city_code, 
      province_code = p.province_code, 
      mother_tongue = p.mother_tongue, 
      educational_attainment = p.educational_attainment, 
      occupational_status = p.occupational_status, 
      parent_type = p.parent_type, 
      deceased = p.deceased, 
      survey_date_conducted = p.survey_date_conducted, 
      person_who_conducted = p.person_who_conducted, 
      parent_dialects = b.name, 
     }); 
    return View(query); 
} 

今、クエリが単に表示はこのように私のテーブルを示しています

My current progress

しかし私が欲しいのは次のようなものです:

The desired result

私は何時間もこれを行う方法を見つけようとしています。ありがとうございました。ここで

答えて

0

似たような

static void Main(string[] args) 
     { 
      var items = Enumerable.Range(0, 10).Select(p => new { Name = "Name" + p%2, LasetName = "LN"+p%2, Dialect = "D"+p }); 

      var data = from item in items 
         group item by item.Name into g 
         select new 
         { 
          Name = g.Key, 
          LastName = g.First().LasetName, 
          Dialect = string.Join(",", g.Select(d=>d.Dialect)) 
         } 
         ; 
      foreach (var item in data) 
      { 
       Console.WriteLine($"Name:{item.Name}, Dialect:{item.Dialect}"); 
      } 
      Console.WriteLine("Done"); 
      Console.ReadLine(); 
     } 

ポストプロセスあなたのグループとvar queryによると、あなたが必要なすべての単一のプロパティの最初のをyouseです。 EFを使用している場合は、最初にToListを実行して連結のためにデータをメモリに取得する必要があります。また、メモリ内のすべての行をプルするデータが多い場合は最適ではありません。

+0

ありがとうございました!出来た。結果を確認するためにLINQPADでコードを実行しようとしました。ほんとうにありがとう! – Fritz

関連する問題