1
執行逗號分隔列表聚集我有一個實體下未能在LINQ
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上分組,失敗的格式應該用逗號分隔。
例如最終的輸出應
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(字符串,則params字符串[])' 不能與訪問一個實例引用;與類型名限定它,而不是
請幫
感謝
如何克服那個開始的逗號。我可以使用String builder eg.group.Aggregate(new Stringbulder(),(a,b)=> a.Append(「,」+ b.FailedFor).tostring())來做同樣的事情。但是在這兩種情況下,額外的逗號都會到達,因此我需要額外執行一些編碼以便將其剝離。是否無法使用string.join以某種方式處理它? – 2013-03-04 02:52:14
@ priyanka.sarkar看我的編輯。 – 2013-03-04 02:52:56