Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How can I auto generate a List<string> with every combination between A00B00 to A100B100?

I think it would be 10,100 items.

So the List would contain:

A00B00
A00B01
A00B02
...
A01B00
A01B01
A01B02
...
A100B98
A100B99
A100B100

It needs to keep the AxxBxx format. I do not want it to be scrambled like 0A01B0.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
211 views
Welcome To Ask or Share your Answers For Others

1 Answer

Since you have a range of 0-100 and you just have A and B as a prefix, We populate two lists each with ints from 0 to 100 and then use System.Linq to find all combinations and append then to a list with a delimiter. Now just replace the delimiter.

There are a bunch of ways you can go about it, I would recommend using System.Linq

public static IEnumerable<string> FindAllCombinationsAnotherWay()
{
    List<int> ListOne = Enumerable.Range(1, 100).ToList();
    List<int> ListTwo = Enumerable.Range(1, 100).ToList();

    var result = ListOne.SelectMany((a, indexA) =>
             ListTwo.Where((b, indexB) =>
                    ListTwo.Contains(a) ? !b.Equals(a) && indexB > indexA
                                      : !b.Equals(a))
                  .Select(b => string.Format("A{0:00}B{1:00}", a, b)));

    return result.Distinct();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...