Compare lists and display on a separate line

Wan

New member
Joined
Nov 7, 2021
Messages
2
Programming Experience
1-3
I have a couple of lists. I want to loop through the lists, compare them and display the output on a separate lines in C#. I'd appreciate input.

For example 'A' can be found between 1-4 and 'B' is listed between 4-7. Therefore, 'A' & 'B' can be found within the range of 1-7. 'C' & 'D' can be acquired within the range 8-9.
'C' occurs between 8 & 9 and 'D' starts at 9.

Input:

List1: '1-4', '4-7', '8-9', '9-10'
List2: 'A', 'B', 'C', 'D'

Output:

1-4 A
4-7 A, B
8-9 C
9-10 C, D

I tried this and not getting the desired output.

List<string> output = new List<string>();
for (int i = 0; i < List1.Count; i++)
{
if (List2.Count <= i + 1)
{
output.Add($"{List1} {List2}");
}
}
 
Please re post your code in code tags. Some of your indexers got lost as the forum software translated [i] as to mean italics. The multi-line code tags is the </> button on the toolbar.
 
C#:
List<string> output = new List<string>();
for (int i = 0; i < List1.Count; i++)
{
if (List2.Count <= i + 1)
{
output.Add($"{List1} {List2}");
}
}
 
The way string interpolation works in C# is that ToString() is called on the object within the curly braces. Most types in C# do not have a specialized implementation of ToString() and so they just default to showing the type name. List1 and List2 are presumably List<string> and therefore won't have any special implementations. This is why you are getting the results that you are currently getting.

Even if you tried to use the Zip() LINQ extension method to correlate the items in both lists, Zip() still won't have the specific behavior you are looking for which is trying to subdivide a range of values into specific zones. To make matters worse, your rules for defining the zones are unusual, to say the least, and so implementing what you want is going to be even harder.
 
Back
Top Bottom