score:3

Accepted answer

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;
}

score:1

you cannot write recursive queries with linq.

score: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)


Related Query

More Query from same tag