private void CreateTree(List<GoodCategory> categories) { var groups = categories.GroupBy(x => x.Parent.Id).ToList(); if (groups.Any()) { foreach (IGrouping<int, GoodCategory> group in groups) { test.Add(group); var items = group.ToList(); CreateTree(items); } } }