4

親子関係(1:n)のテーブルを含むselectステートメントを使用してカスタムエンティティを作成しようとしています。選択したオブジェクトをグループ化しようとしましたが、解決策を見つけるのに問題があります。

私のデータは次の形式になります。

ParentTable
Userid | Col1 | Col2 | ... | Coln

ChildTable:
Id | Userid | Col1 | Col2 | ... | Coln

私が作成しようとしている結果オブジェクトは次のようになります。

Custom { UserID, IEnumerable<ChildTable> }

私はクエリを書きました:

    from parent in db.ParentTable
    join child in db.ChildTable on new { Userid = parent.Id } equals new { Userid = child.Userid } into child_join
    from child in child_join.DefaultIfEmpty()
    where
         child.Col1 == Argument1 &&
         child.Col2 == Argument2
    select new {
         Username = parent.Username,
         child.Col1,
         child.Col2,
         child.Col3,
         child.Coln 
   }

これにより、次の結果が返されます。

Username | Child Col1 | Child Col2 | Child Col3 | .. |Child Coln
asdf        1              11           22        ..     33
asdf        2              22           33        ..     44
asdf        3              33           44        ..     55
qwer        4              44           55        ..     66
qwer        5              55           66        ..     77
qwer        6              66           77        ..     88
zxcv        7              77           88        ..     99
zxcv        8              88           99        ..     00

私はこれを達成したいと思います:

Username | IEnumerable<Child>
asdf     |     {{1             11           22        ..     33}
         |      {2             22           33        ..     44}
         |      {3             33           44        ..     55}}
qwer     |     {{4             44           55        ..     66}
         |      {5             55           66        ..     77}
         |      {6             66           77        ..     88}}
zxcv     |     {{7             77           88        ..     99}
                {8             88           99        ..     00}}

ユーザー名でアイテムをグループ化し、Custom {UserID、IEnumerable}の形式でカスタムオブジェクトを作成しようとしています。このオブジェクトは、完了したらシリアル化できます。どんな助けでも大歓迎です。編集:データ構造に関しては、サードパーティのシステムに接続しているため変更できないため、与えられたもので作業しています。

4

2 に答える 2

4

ここで探しているのはサブクエリかもしれません。私はこのようなことを試みます:

from parent in db.ParentTable
select new {
  Username = parent.Username,
  Children = (from child in db.ChildTable
              where child.UserId == parent.UserId
              select child)
}

ループせずにgroupbyクエリからリストにデータを入力するだけの場合は、次のように実行できます。

List<ResultDataClass> returnData = dataResult.GroupBy(item => new KeyValuePair<int, string>(item.UserID, item.UserName), item => item)
                            .Select(rdc => var newdata = new ResultDataClass(userData.Key.Key, userData.Key.Value, userData.Select(item => item.TrackData))).ToList();
于 2013-01-31T02:30:18.713 に答える
0

これは理想的な解決策ではありませんが、クエリを実行した後、データをシリアル化する前に、オブジェクトをグループ化することにしました。'group by'句を適用しようとしましたが、子アイテムの最大値または合計を検索しようとしておらず、ストレートグループを検索しようとしていないため、適用する集計がありません。私の(洗練されていない)ソリューションは、元のクエリを使用してそのように見えますが、カスタムエンティティクラスに配置されています:

from parent in db.ParentTable
join child in db.ChildTable on new { Userid = parent.Id } equals new { Userid = child.Userid }
where
      child.Col1 == Argument1 &&
      child.Col2 == Argument2
select new ChildDataDomainModelClass {
      Username = parent.Username,
      child.Col1,
      child.Col2,
      child.Col3,
      child.Coln 
}

 //I had grouped the object like this, where the object type returned on the grouping is of type IEnumerable<IGrouping<KeyValuePair<int, string>, ChildDataDomainModelClass>>:

List<ResultDataClass> returnData= new List<ResultDataClass>();
var groupedByUserName = dataResult.GroupBy(item => new KeyValuePair<int, string>(item.UserID, item.UserName), item => item);
        foreach (var userData in groupedByUserName)
        {
             var newdata = new ResultDataClass(userData.Key.Key, userData.Key.Value, userData.Select(item => item.TrackData)); //TrackData is what I have named the Child Data that was returned
             returnData.Add(newdata);
        }

私は提供された入力に感謝します。カスタムクラスを作成する前にオブジェクトをループする必要がない、この問題に対するより洗練された解決策を誰かが持っている場合は、私に知らせてください。理想的には、「ChildDataDomainModelClass」を作成する必要はなく、クエリを通じて新しい「ResultDataClass」を選択します。これがこの問題に遭遇した他の人に役立つことを願っています。解決策を思いついたら、それに応じてこの投稿を更新します。皆様のご意見に感謝いたします。ありがとう。

于 2014-08-14T22:53:20.327 に答える