0

我正在尝试根据查询结果是否包含字符串“baby”来过滤查询结果。

IEnumerable<ICD.ViewModels.HomeSearchViewModel> query =
                ICDUnitOfWork.AlphaGroups.Find().GroupJoin(ICDUnitOfWork.Alphas.Find(),
                                                           a => a.AlphaGroupID,
                                                           g => g.AlphaGroupID,
                                                           (alphaGroups, alphas) =>
                                                           new ICD.ViewModels.
                                                               HomeSearchViewModel
                                                               {
                                                                   AlphaGroups =
                                                                       alphaGroups,
                                                                   Alphas = alphas
                                                               })
                    .Where(row =>
                           row.AlphaGroups.Title.Contains("baby")
                           || row.Alphas.Any(alpha => alpha.Title.Contains("baby"))
                    );

问题是当 Alpha.Title 包含字符串“baby”时,它应该只显示包含“baby”的 Alpha,而不是 AlphaGroup 中的每个 alpha。如果 AlphaGroup.Title 包含“婴儿”,它应该继续显示组中的每个 alpha。我怎样才能做到这一点?

4

1 回答 1

1

您可以尝试以下方法:

IEnumerable<ICD.ViewModels.HomeSearchViewModel> query =
                ICDUnitOfWork.AlphaGroups.Find()
                             .GroupJoin(
                                   ICDUnitOfWork.Alphas.Find()
                                                .GroupBy(a => new 
                                                                {
                                                                  BabyIndicator = a.Title.Contains("baby"),
                                                                  GroupID = a.AlphaGroupID
                                                                }),
                                   a => a.AlphaGroupID,
                                   g => g.Key.GroupID,
                                   (alphaGroups, alphas) =>
                                      new ICD.ViewModels.HomeSearchViewModel()
                                        {
                                          AlphaGroups = alphaGroups,
                                          Alphas = alphaGroups.Title.Contains("baby") ?
                                            alphas.Select(g => g.AsEnumerable()).Aggregate((g1,g2) => g1.Concat(g2)) :
                                            alphas.Aggregate(
                                            (g1,g2) => g1.Key.BabyIndicator ?
                                                       g1 :
                                                       g2).AsEnumerable()
                                        })

逻辑:

问题是当 Alpha.Title 包含字符串“baby”时,它应该只显示包含“baby”的 Alpha,而不是 AlphaGroup 中的每个 alpha。

Here we group the alphas by groupID and whether they have babies, we then group join this onto the alphGroups. So we have four possibilities, no groups, one group without baby, one group with only babies and one of each. To pull this all together we aggregate. If there are no groups it returns no groups, if there is one group it returns that group, if there are two groups it returns only the one with babies.

If the AlphaGroup.Title contains "baby" it should continue to show each alpha in the group.

Here we check whether an alphaGroup has a title baby, if it does return the whole grouping, if not apply alpha title logic

于 2012-11-14T08:52:47.093 回答