0

A rather simple question, but I can't work out how to convert a nested loop of such a kind:

for (var a = 0; a < n; a++)
{
    for (var b = a + 1; b < n; b++)
        {
            //let here be a creation of a Tuple
            list.Add(Tuple.Create(a, b))
        }
}

to a LINQ-statement

upd:

I tried something like this:
from a in Enumerable.Range(0, n)
from b in Enumerable.Range(1, n)
...

but it didn't work

8
  • 1
    I assume you're looking for list.AddRange(resultOfUsingLinq)? In any case, I love LINQ, but I have serious doubts you're going to end up with a LINQ solution that is going to be easier to read than what you have with your loops. Commented Jun 6, 2022 at 19:49
  • I am sure it will be less clear, but just in case I need to use a linq for this Commented Jun 6, 2022 at 19:50
  • Why would you "need" to use LINQ? Is this for a school assignment? (nothing wrong with that, but that's the only context in which such a requirement could possibly make sense) Commented Jun 6, 2022 at 19:51
  • Well, I'm trying to rewrite an interesting maths algorithm using only LINQ, so it's a kind of question of pure interest:) Commented Jun 6, 2022 at 19:54
  • Thanks for responding. That's fair enough. Commented Jun 6, 2022 at 19:54

3 Answers 3

2

Alright, here is one solution using Linq extension methods.

var list = Enumerable.Range(0, n-1)
    .SelectMany(
        a => Enumerable.Repeat(a, n-1 - a)
                .Zip( Enumerable.Range(a+1, n - (a+1)) )
    ).ToList();

How does it work? Well, look up the documentation for the involved Linq methods, that should provide sufficient insight to what's going on here with that ugly Linq construct of mine. (Well, it's not only ugly, it's also so slow you are not at any risk of violating any speed limit...)

Note that the generated list is of type System.Collections.Generic.List<(int First, int Second)>, which is using C# tuples, which are a value types and not to be confused with the System.Tuple type (which is a reference type) you used in the code in your question.


And here is a solution involving Linq query syntax:

var list = (
    from a in Enumerable.Range(0, n - 1)
    from b in Enumerable.Range(a + 1, n - (a + 1))
    select (a, b)
).ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

@TheBadBat, i just added an example with Linq query syntax. Seems my headache wasn't that bad already :-P
2

It could be helpfull.

var result = (from a in Enumerable.Range(0, n - 1)
              from b in Enumerable.Range(a + 1, n - a - 1)
              select Tuple.Create(a, b)).ToList();

3 Comments

Tuple.Create will return Tuple instead of ValueTuple and should be avoided for new code due to unnecessary memory pressure. Instead, just select (a, b). EDIT: I see you were basing your code on the OPs question. I've replicated the comment in the OP.
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
The @Community bot is worse than useless sometimes.
-1

You may use this: Enumerable.Range(0, n-1).ToList().ForEach(x => Enumerable.Range(x + 1, n-x-1).ToList().ForEach(y => list.Add(Tuple.Create(x,y))));

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.