2014-12-06 52 views
1

我有類Product從表2場選擇並加入他們作爲字符串

public class Product 
{ 
    public string Name{get;set;} 
    public int Quantity{get;set;} 
    // and other 
} 

一些操作,我得到List<Product>後。

如何從列表中創建格式爲productName1(productQuantity1), productName2(productQuantity2), ...的字符串?

我應該使用String.Join,但不能得到如何使用它與一些動態對象或KeyValuePair(如果我使用Select獲得這樣的對象)。

回答

0
String.Join(", ", list.Select(p => String.Format("{0}({1})", p.Name, p.Quantity))) 
+0

謝謝,實在想不出它是如此容易 – user3625486 2014-12-06 21:49:35

0
string.Join(", ", products.Select(p=>string.Format("{0}({1})",p.Name, p.Quantity))) 
0

一種可能的方法:

覆蓋ToString()功能Product類:(只執行)

return string.Format("{0}({1})", Name, Quantity); 

然後寫:

string.join (",", products.Select(p => p.ToString()));