top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Is it possible create recursive linq?

+4 votes
507 views

How can i create recursive linq (vb.net)?

posted Mar 3, 2014 by Muskan

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+2 votes
 
Best answer

LINQ to objects doesn't really do recursion. If you want to go some constant number of levels deep, you can build a monstrosity of a statement that would work, but if the requirement is to go some unknown number of levels deep I don't think there's a solution. I have some vague beginnings of the kind of thing that might work, but it's horrible and I'd be ashamed to recommend it.

If you really wanted to use LINQ, I'd say the best solution would be to implement IEnumerable(Of T) on whatever your collection is. This would involve implementing an IEnumerator(Of T), and you'd write that such that it recursively descends. LINQ works great with types that are enumerable so this would dramatically reduce the complexity of the statement.

Before I walk down either of those paths, I'd like to ask if there's a reason you feel like you need to use LINQ. Generally there's no benefit to using it over the traditional approach to a problem other than perhaps a more elegant expression of the intent; if the statement is hard to write and understand it's usually better to not use LINQ at all.

answer Mar 7, 2014 by Asmita Agrawal
Similar Questions
+2 votes

How to use Zip operator in Linq with .NET 4.0?

+4 votes

How to Generic Method to Create and Execute Parameterized SQL Query in .NET 4.0 ?

+7 votes

I want to create a CAPTCHA security image for my web application.
Can anyone provide with the code for the same.

...