实现效果
List<string> list1=new List<string>{"A","B","C"};
List<string> list2=new List<string>{"=","<", ">"};
List<string> list3=new List<string>{"1","2","3"};
合并成:
A=1 AND B<2 AND C>3
1、通过for循环实现
StringBuilder str = new StringBuilder();
for(int i=0; i< list1.count; i++)
{
str.AppendFormat("{0}{1}{2} AND ", list1[i], list2[i], list3[i]);
}
str.Length = str.Length -5;
string final = str.ToString();
2、使用MoreLINQ实现
MoreLINQ文档:MoreLINQ
var res = string.Join(" AND ", list1.EquiZip(list2, list3, (x, y, z) => x + y + z));
3、使用Enumerable.Range和Select
string b = string.Join(" AND ", Enumerable.Range(0, list1.Count)
.Select(i => $"{list1[i]} {list2[i]} {list3[i]}"));
或者
string.Join(" AND ", list1.Select((e1, idx) => $"{e1} {list2[idx]} {list3[idx]}"));
4、使用Linq中的Zip()方法
string result = string.Join(" AND ", list1.Zip(list2, (l1, l2) => l1 + l2).Zip(list3, (l2, l3) => l2 + l3));