1

Suppose, we have a line of code

item.GetChildren().Where(i => i.Name == "something");

where item is of type Sitecore.Data.Items.Item.

How does it work?

1) after .GetChildren() call a query to database gets invoked, results returned, and then .Where(i => i.Name == "something") happens on client side

or

2) .Where(i => i.Name == "something") gets appended to the database query and only after that the database query gets invoked and results returned?

or both options aren't correct?

  • What is the type that is returned by `GetChildren`? – Arran Sep 28 '17 at 15:51
  • That depends on what `GetChildren()` is. – Willem Van Onsem Sep 28 '17 at 15:52
  • 1
    The [documentation](https://doc.sitecore.net/sitecore%20experience%20platform/developing/developing%20with%20sitecore/search%20and%20item%20buckets/linq%20to%20sitecore) has a good explanation. To be clear I am not suggesting that asking questions about documented features is inappropriate but in this case the documentation seems very clear and it _seems_ unlikely that a better answer will be provided here. – Jason Boyd Sep 28 '17 at 15:52
  • 1
    See: https://stackoverflow.com/questions/2876616/returning-ienumerablet-vs-iqueryablet – Xiaoy312 Sep 28 '17 at 15:53
  • @JasonBoyd documentation is about performing LINQ on IQueryable returned by SearchContext. I ask about performing LINQ on item.GetChildren(), which returns ChildList. And these work differently, I believe. – Jack Russell Sep 28 '17 at 16:02
  • @Arran Sitecore.Collections.ChildList : IEnumerable, ICollection, IEnumerable – Jack Russell Sep 28 '17 at 16:03
  • I see. Perhaps you can use Fiddler to inspect the response returned from SiteCore to see if it does or does not contain items that you expect to be filtered out. – Jason Boyd Sep 28 '17 at 16:48

1 Answers1

2

The GetChildren call will return all Children of the item and then the Linq statement will filter the item collection based on your predicate. It is not done at a database query level, but it is done on the web server (I assume this is what you mean when you say client side, and not browser side).

To break it down, Sitecore.Data.Items.Item.GetChildren() calls Sitecore.Data.Managers.ItemProvider.GetChildren() which is the default item provider set via config/DI. This makes a query to the database. This returns Sitecore.Collections.ChildList which as you point out inherits from IReadOnlyList<Item>, IReadOnlyCollection<Item>, IEnumerable<Item>, IEnumerable, ICollection.

The Linq statement then filters the items, using the Where extension method. This is for all practical reasons similar to using a foreach loop, adding only the items which match your predicate and then returning that list.

Depending on what you are doing, how many items you have, if you have followed best practice of not having more than 100 child items per node then it shouldn't cause any significant performance issues.

jammykam
  • 16,940
  • 2
  • 36
  • 71
  • Thanks! Exactly the answer I was waiting for. I guess, the only way to build database query using LINQ statements in Sitecore is ContentSearchManager.GetIndex(item).CreateSearchContext().GetQueryable().Where(...)? – Jack Russell Sep 28 '17 at 18:29
  • Yes, that's the best and most performant way usually. There's a couple bits to hook up for the predicate builder but suggest looking at [this](http://www.bekagool.com/news-and-insights/sitecore-7-search-a-quickstart-guide) or [this](http://www.mattburkedev.com/sitecore-7-contentsearch-tips/) as a starting point – jammykam Sep 28 '17 at 19:45