0

2 つのデータベース テーブルがあります。1 つはマスター レコード、もう 1 つはディクショナリ構造を使用した詳細レコードです。

同じヘッダー ID を持つ複数の行を返す detailstable からいくつかのフィールドをクエリしたい。

両方のテーブルを結合する linq を使用してカスタム レコードを作成する方法はありますか

マスターテーブル: ID MasterName Date ...

詳細テーブル ID MasterID キー値

疑似コード: マスターから context.mastertable 結合の詳細を context.detailstable on master.ID == detail.masterID SELECT new CustomClass{ ID = master.ID, Name = master.MasterName Customfield = (detailsvalue.where key == "customfield ") + (detailvalue.where キー == "customfield2") };

誰かが私を助けてくれることを願っています。

grtz Luuk Krijnen

4

2 に答える 2

1

Join() メソッド内で作成された匿名型を使用できます。

  List<Master> list1 = new List<Master>(){
    new Master(){ Id=1, Name="Name1"},
    new Master(){ Id=2, Name="Name2"}};

  List<Detail> list2 = new List<Detail>(){
    new Detail(){ Id=1, MasterId=1, Description="Description1"},
    new Detail(){ Id=2, MasterId=1, Description="Description2"},
    new Detail(){ Id=3, MasterId=1, Description="Description3"},
    new Detail(){ Id=4, MasterId=2, Description="Description4"},
    new Detail(){ Id=5, MasterId=2, Description="Description5"}};

  // IEnumerable of anonymous types
  var result = list1.Join(list2, m => m.Id, d => d.MasterId, (m, d) => new { Id = m.Id, Name = m.Name, Description = d.Description });

  foreach (var item in result)
    Console.WriteLine(item.Id + " " + item.Name + " " + item.Description + Environment.NewLine);

  // Returns
  // 1 Name1 Description1
  // 1 Name1 Description2
  // 1 Name1 Description3
  // 2 Name2 Description4
  // 2 Name2 Description5
于 2012-11-21T23:41:24.790 に答える
0

このようなものは機能しませんか?:

var query = from master in context.masterTable
            join detail in context.detailTable
            on master.Id == detail.masterId
            where detail.Key == "customField" || detail.Key == "customField2"
            select new
            {
                id = master.Id,
                name = master.Name,
                customField = detail.Key
            };

そうでない場合は、正確に何を探しているのかをもう少し詳しく説明してください。IE データがどのように保存されているか、およびこのクエリから得たい最終結果について説明してください。

于 2012-11-21T23:29:28.173 に答える