1

私は以下のようなエンティティを持っています

public class A
    {
        public string TestMethodName { get; set; }
        public string FailedFor { get; set; }
    }

私は下に住んでいて、それから組合を行っています

            List<A> aCollection = new List<A>();
            List<A> bCollection = new List<A>();

            aCollection.Add(new A { TestMethodName = "Method1", FailedFor = "DPLServerURL" });
            aCollection.Add(new A { TestMethodName = "Method2", FailedFor = "DPLServerURL" });
            aCollection.Add(new A { TestMethodName = "Method3", FailedFor = "DPLServerURL" });
            aCollection.Add(new A { TestMethodName = "Method4", FailedFor = "DPLServerURL" });


            bCollection.Add(new A { TestMethodName = "Method1", FailedFor = "OrderXmlLocation" });
            bCollection.Add(new A { TestMethodName = "Method2", FailedFor = "OrderXmlLocation" });
            bCollection.Add(new A { TestMethodName = "Method5", FailedFor = "OrderXmlLocation" });

            var kk = aCollection.Union(bCollection);

私が探しているのは、TestMethodNamesとFailedForのgroupbyをコンマで区切る必要があるということです。

たとえば、最終的な出力は次のようになります

Method1  DPLServerUrl,OrderXmlLocation
Method2   DPLServerUrl,OrderXmlLocation
Method3   DPLServerUrl
Method4   DPLServerUrl
Method5   OrderXmlLocation

私の試み

 var mm  = kk.GroupBy(g => g.TestMethodName)
                  .Select(group =>
                        new
                        {
                            Name = group.Key,
                            AggregateFailedFor= group.Aggregate("",(a, b) => a.Join(",",b.FailedFor))
                        });

コンパイル時エラーが発生します

メンバー'string.Join(string、params string [])'はインスタンス参照ではアクセスできません。代わりにタイプ名で修飾してください

助けてください

ありがとう

4

1 に答える 1

2

エラーは一目瞭然です。Join静的です..したがって、インスタンス変数で呼び出すことはできません。タイプ名で呼び出す必要があります (例string: ):

Students = group.Aggregate("", (a, b) => string.Join(",", a, b.FailedFor))
//                                       ^^^^^^ this

最初の呼び出しは「コンマを使用して何も結合せずに FailedFor を結合する」ことになり、結果が「,FailedFor」になるため、集計では結果の先頭にコンマが配置されることに注意してください。

編集:

コードを次のように変更しました。

kk.GroupBy(g => g.TestMethodName)
    Select(group =>
        new
        {
            Name = group.Key,
            Students = group.Aggregate("", (a, b) => 
            {
                if (string.IsNullOrEmpty(a))
                {
                    return b.FailedFor;
                }
                else 
                {
                    return string.Join(",", a, b.FailedFor);
                }
            })
        });

..そしてこれが結果です(あなたが言ったのはあなたがしたことです):

クエリ結果

于 2013-03-04T02:45:38.253 に答える