2

Can anyone tell me how to best possible way to convert the following code to LINQ:

static int MyMethod(MyClass my, bool b)
{
  int cnt = 0;

  foreach(SomeClass cls in my.SomeMethod()
  {
    cnt = cnt + cls.length;
  }

  if(b == true)
  {
    foreach(MyClass aa in my.SomeOtherMethod())
    {
      cnt = cnt + MyMethod(aa, true); // recursive
    }
  }

  return cnt;
}

Please see that I know that the code above works fine, but I need to write it in LINQ and compare.

3 Answers 3

3

As Darin says, you can't easily recurse without doing so explicitly - but you can still simplify this code with LINQ:

static int MyMethod(MyClass my, bool b)
{
  int cnt = my.SomeMethod().Sum(cls => cls.length);    
  if (b)
  {
      cnt += my.SomeOtherMethod().Sum(aa => MyMethod(aa, true));
  }    
  return cnt;
}
Sign up to request clarification or add additional context in comments.

Comments

2

First of all, you should use the solution by Jon, because it is the most straightforward and readable soltion you can get. However, you can write recursive Func<...> declarations in C#, so the code could be rewritten like this:

Func<MyClass, bool, int> myFoo = null;
f = (my, b) =>
    my.SomeMethod().Sum(cls => cls.length) +
    (b ? my.SomeOhterMethod().Sum(aa => myFoo(aa, true)) : 0);

This is essentially the same thing as what Jon posted, with the exception that I'm using Func<..> instead of a full method. (This can be useful for encoding recursion in LINQ queries, but I think it is useful only very rarely)

2 Comments

You need to give myFoo a value to start with - typically null - so that it's definitely assigned before the second statement. I think you meant to assign to myFoo, too :) Obviously the F# version would be somewhat neater...
@Jon: Yes, I wanted to initialize it with null (because the C# compiler uses the current value, it will not use null during the recursive call). The F# version would be a bit simpler because F# syntax for "functions" is more lightweight, but it is essentially the same (unless you use tricks like Y combinator, but that's not very practical).
1

You cannot write recursive queries with LINQ.

1 Comment

Oh, you can - they're just excruciating: blogs.msdn.com/b/madst/archive/2007/05/11/…

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.