-5

User can pass any number of list with same number of elements in it. Example- user has passed below 3 (could be dynamic with same number of elements in it) list -

hospitalId - H11, H12, H13...n
patientId - P11, P12, P13...n
statusId - S11, S13, S11...n

What is the efficient way of creating a set out of it and storing it as a string in below format? Need a c# code for it.

expected output -

"((H11,P11, S11), (H12, P12, S13), (H13, P13, S11))" 
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Achintya
  • 11
  • 4
  • 3
    "Need a c# code for it." Of course you need. And what did you try to achieve this? Where are you stuck? We´re not doing your job for you. – MakePeaceGreatAgain Jul 06 '18 at 09:25
  • Possible duplicate of [How to combine more than two generic lists in C# Zip?](https://stackoverflow.com/questions/10297124/how-to-combine-more-than-two-generic-lists-in-c-sharp-zip) – mjwills Jul 06 '18 at 09:26
  • yes I tried, wanted to understand how you iterate when the number of list is dynamic. please see my comment below. you can also redirect me to refer any site if the approach to solve this is already discussed. Thanks – Achintya Jul 06 '18 at 09:41
  • how do you declare hospitalId, patientId and statusId? – Mark Jul 06 '18 at 09:45
  • User is sending a parameter list, could be different at different time. The format is Key value Dictionary> e.g.[ HospitalId, (H11, H12, H13) ] – Achintya Jul 06 '18 at 09:50

2 Answers2

2

You should iterate through your list and append them index wise to prepare the result.

StringBuilder builder = new StringBuilder();
builder.Append("(");
for(var index = 0; index < n; index++) 
{
  builder.AppendFormat("({0}, {1}, {2})", hospitalId[index], patientId[index], statusId[index]);
}
builder.Append(")");

var result = builder.ToString();
Ipsit Gaur
  • 2,872
  • 1
  • 23
  • 38
  • 1
    IMHO code-only answers are a smell. In particular when no afford was given within the question. – MakePeaceGreatAgain Jul 06 '18 at 09:32
  • Thanks for responding, I am aware of above approach and will definitely work if you know the number of list, but in my case user can pass any number of list, for example along with (hospital, patient and status) id next request could come along with one more list e.g. operation_Id list having elements till n. – Achintya Jul 06 '18 at 09:39
0

If you have n number of List<T> items with the same length, a basic loop will do the trick. Here's a version as an extension method that will take any number of lists as an input:

public static IEnumerable<IEnumerable<T>> ZipMultiple<T>(this List<List<T>> source)
{
    var counts = source.Select(s => s.Count).Distinct();
    if (counts.Count() != 1)
    {
        throw new ArgumentException("Lists aren't the same length");
    }

    for (var i = 0; i < counts.First(); i++)
    {
        var item = new List<T>();
        for (var j = 0; j < source.Count; j++)
        {
            item.Add(source[j][i]);
        }
        yield return item;
    }
}

After that, it's pretty simple to convert the result into a string in another loop, or you can do it as a single liner:

var zipped = lists.ZipMultiple();

var output = $"({string.Join(", ", zipped.Select(x => $"({string.Join(",", x)})"))})";
DavidG
  • 113,891
  • 12
  • 217
  • 223