用linq连接对象

时间:2014-12-04 10:42:49

标签: linq

我有一个这样的课程

public class test{
        public string a { get; set; }
        public string b { get; set; }
}

和对象列表

List<test> list= new List<test>();
list.Add(new test() {a = "1a", b = "1b" });
list.Add(new test() {a = "2a", b = "2b" });
...

我希望连接元素并使用类似

的字符串
"1a 1b + 2a 2b" ... etc

我可以使用Linq来做这件事吗?

2 个答案:

答案 0 :(得分:1)

这可以通过映射到字符串在Linq中完成,如下所示。

string Result =
    String.Join(" + ", 
        list.Select(iObj => String.Format("{0} {1}", iObj.a, iObj.b)));

答案 1 :(得分:0)

String.Join(" + ", list.Select(x => /*What you want*/)); 

会做到这一点!