简体   繁体   中英

Filtering query based on condition

I am trying to filter the results on my query based on whether or not they contain the string "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"))
                    );

The problem is that when an Alpha.Title contains the string "baby" it should only show the Alpha's that contain "baby", rather than every alpha in the AlphaGroup. If the AlphaGroup.Title contains "baby" it should continue to show each alpha in the group. How can I accomplish this?

You could try something like the following:

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()
                                        })

Logic:

The problem is that when an Alpha.Title contains the string "baby" it should only show the Alpha's that contain "baby", rather than every alpha in the AlphaGroup.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM