public IQueryable <TreeViewItem> GetChildren( int id, int rootDataItemId = 0, TreeViewItem.GetCountsType countsType = TreeViewItem.GetCountsType.None) { var qry = _dataItemService.GetChildren(id, rootDataItemId); List <DataItemViewModel> dataItemEntityList = new List <DataItemViewModel>(); List <TreeViewItem> groupNameList = new List <TreeViewItem>(); var groups = qry.OrderBy(g => g.ItemName); foreach (var group in groups) { dataItemEntityList.Add(group); var treeViewItem = new TreeViewItem(); treeViewItem.Id = group.Id.ToString(); treeViewItem.Name = $"{group.ItemName}({group.ItemCode})"; treeViewItem.IsActive = (group.EnabledMark ?? 0) > 0; treeViewItem.IconCssClass = "fa fa-sitemap"; if (countsType == TreeViewItem.GetCountsType.ChildGroups) { treeViewItem.CountInfo = this._dataItemService.GetAlls().Where(a => a.ParentId.HasValue && a.ParentId == group.Id).Count(); } groupNameList.Add(treeViewItem); } //快速找出哪些项目有子级 List <long> resultIds = dataItemEntityList.Select(a => a.Id).ToList(); var qryHasChildrenList = _dataItemService.GetAlls() .Where(g => g.ParentId.HasValue && resultIds.Contains(g.ParentId.Value)).Select(g => g.ParentId.Value) .Distinct() .ToList(); foreach (var g in groupNameList) { int groupId = g.Id.AsInteger(); g.HasChildren = qryHasChildrenList.Any(a => a == groupId); } return(groupNameList.AsQueryable()); }