1
public class MyClass
{
    public MyClass();

    public long Id { get; set; }
    public intScore { get; set; }
    public MyClass[] subclasses { get; set; }
    public string title { get; set; }
   .....    
 }

The results returned from extrenal source are recursive, I am trying to retrieve the results from the collection using linq or any extension methods using recursion, any help appreciated.

ex; The collection i have is

Myclass results=XXXmethod(xxxx)// which gives me results.

subclasses is a list of type Myclass, so this list will have again some collection, and inturn that can collection agian nested levels

say the service returned 10 results or Myclass[10] Myclass[0] is having again Myclass[4] which might have 2 or 4, need to build a collection allitems

I am trying to do like this but some are missing

results.class.subclusters.subclasses (o => o.subclasses )
               .SelectMany(x => x.subclasses ).ToList()

but which is not giving correct results.

cuongle
  • 74,024
  • 28
  • 151
  • 206
Praveen
  • 100
  • 8

1 Answers1

3

LINQ itself does not support recursive method, but you can have the recursive method like below:

public class MyClass
{
    public int Id { get; set; }

    public MyClass[] SubClasses { get; set; }

    //More properties

    public IEnumerable<MyClass> GetRecursive()
    {
        yield return this;

        if (SubClasses != null)
        {
            foreach (var item in SubClasses
                          .Where(s => s != null)
                          .SelectMany(x => x.GetRecursive()))
            {
                yield return item;
            }
        }
    }
}

Then call:

myClass.GetRecursive()
sloth
  • 99,095
  • 21
  • 171
  • 219
cuongle
  • 74,024
  • 28
  • 151
  • 206
  • you might want to add a null filter in there: SubClasses.Where(x => x != null).SelectMany(...) – staafl Aug 25 '12 at 09:08
  • I am trying to do some thing similar, but not sure how yield works here, Other thing is dont have structure.. i have the results returned as a object of myclass, just wondering how can i call myclass.Getrecursive(), getrecursive should take mylist and return all the children right? Any help appreciated – Praveen Aug 25 '12 at 09:11
  • @PraveenKonduru: What do you mean "dont have structure" and "the results returned as a object of myclass"? – cuongle Aug 25 '12 at 09:13
  • I am consuming a service which is returning me these results, and the results are of type. Ofcourse the code what you have sent my results structure is same.Another thing is tried ur code yield return this; here what is this?-- Thanks – Praveen Aug 25 '12 at 09:24
  • @PraveenKonduru: `yield return this` is the key for you to get the result, you might need to look more you yield: http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx – cuongle Aug 25 '12 at 09:29
  • appreciate your quick responses basically its throwing me an excpetion. I was trying in a console appln. thanks. – Praveen Aug 25 '12 at 09:44
  • @PraveenKonduru: what kind exception you have? – cuongle Aug 25 '12 at 09:48
  • @Praveen: You are trying to return a ClusterService object in a method that returns Cluster. – Şafak Gür Aug 25 '12 at 11:41