c# - LINQ to determine if children exist -


For simplicity, the "block" object has the following properties:

  SectionId ParentSectionId name  

I currently have to get the section of the section of the given section The following is the LINQ code for:

  list & lt; Section & gt; Classes = catches section. GetAllSections (); Classes ANALIMABLE (). Where (s = & gt; s.ParentSectionId == 10);  

It gives me all the children in the section with Section 1 of 10 (again, for simplicity), but I need to include just those sections to pursue it Those who have children in their own are in SQL, I can do something like:

  SELECTId, Section.ParentSectionId, Join Section Inner from Section.Name. ParentSectionId = Section.SectionId where section ParentSectionId = 10 groups by section SectionId, Section Parent Science ID, section. No. Hoving COUNT (Kids Size Id) & gt; 0  

How can I get it in LINQ / What is the best way to get it with LINQ?

Thanks

There are a few ways to do this (they are very similar)

  IEnumerable & lt; Section & gt; Query = classes Where (S = & gt; class Anyone (C => s.SectionId = c.ParentSectionId))); IEnumerable & LT; Section & gt; Query = Select from those sections (from where c in section where c.ParentSectionId == s.SectionId) c). Select any ();  

Or better:

  ILKupupe  c.ParentSectionId); IEnumerable & LT; Section & gt; Query = classes Where (s => child look [s.SectionId] .no ());  

GroupGone technology, which should also be very efficient:

  IEnumerable & lt; Section & gt; The questions from s = classes are included in sections on s.SectionId. Equals c.ParentSectionId in those children where children Select any ();  

Comments