15

I have three string list, the purpose is combine these list to a single string with separator.

List<string> list1=new List<string>{"A","B","C"};
List<string> list2=new List<string>{"=","<", ">"};
List<string> list3=new List<string>{"1","2","3"};

The final output is like following:

A=1 AND B<2 AND C>3

Is there any easy way to generate the final string? I used for loop, but it seems to be ugly. I know C# string has Join method to combine an array with separator. How to combine multiple arrays with separator?

Below is my code:

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();
4
  • I realise it doesn't really answer your question, but your problem might be more easily solved if the data was in a multi (2)-dimensional array; or your three lists were all contained inside a parent list Commented Mar 29, 2018 at 7:25
  • 1
    Using a for loop for this is absolutely fine. If you really think it looks too ugly you can still wrap it into a seperate method to "hide" the loop in your code. Commented Mar 29, 2018 at 7:27
  • 8
    You are using lists for everything? Why not a single List<Comparison> with properties for Operator, Variable and Value? No need to join multiple collections via index, that's most times a code smell that begs for refactoring. This class could override ToString to return what you ask for. Commented Mar 29, 2018 at 7:42
  • str.Length = str.Length -5; doesn't work str.Length is read only Commented Mar 29, 2018 at 8:20

4 Answers 4

24

Use Linq Zip() twice:

string result = string.Join(" AND ", list1.Zip(list2, (l1, l2) => l1 + l2).Zip(list3, (l2, l3) => l2 + l3));

https://dotnetfiddle.net/ZYlejS

Sign up to request clarification or add additional context in comments.

Comments

10

You could use a combination of string.Join and linq:

string.Join(" AND ", list1.Select((e1, idx) => $"{e1} {list2[idx]} {list3[idx]}"));

1 Comment

Just take care that all listst really have the same size.
3

You could use one of overloads EquiZip() in MoreLINQ:

var res = string.Join(" AND ", list1.EquiZip(list2, list3, (x, y, z) => x + y + z));

Comments

1

You could also use a combination of string.Join along with Enumerable.Range like so:

string result = string.Join(" AND ", Enumerable.Range(0, Math.Min(list1.Count, 
                                    Math.Min(list2.Count, list3.Count)))
                      .Select(i => $"{list1[i]} {list2[i]} {list3[i]}"));

if the lists are guaranteed to have the same size then it can be reduced to:

string b = string.Join(" AND ", Enumerable.Range(0, list1.Count)
                 .Select(i => $"{list1[i]} {list2[i]} {list3[i]}"));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.