2

我有一个返回如下结果的 GroupJoin 查询:

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

Alphas 是IEnumerable,一个 AlphaGroup 包含多个 Alphas。他们都有一个名为“Title”的属性。我需要根据包含字符串“baby”的“Title”属性过滤结果。我怎样才能做到这一点?

编辑:我还应该提到,如果特定的 alpha 组标题包含“婴儿”,则应显示所有具有 AlphaGroupID 的相应 Alpha。

4

2 回答 2

1

如果您使用的是 Entity Framework >= 4,则Contains可以:

.Where(row => 
    row.AlphaGroups.Title.Contains("baby") 
        || row.Alphas.Any(alpha => alpha.Title.Contains("baby"))
 );

编辑已更新以预过滤分组中的 alpha。最终过滤器排除不包含 alpha 的行(表明AlphaGroup标题和任何Alpha 都不包含该字符串)。

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(alpha 
                    => alphaGroups.Title.Contains("baby")
                        || alpha.Title.Contains("baby"))
            });
    .Where(row => row.Alphas.Any());
于 2012-11-13T17:46:16.497 回答
1

建议反对.Any()as can 映射到嵌套联合,而只是过滤预加入

尝试

IEnumerable<ICD.ViewModels.HomeSearchViewModel> query = 
     ICDUnitOfWork.AlphaGroups.Find()
                  .Where(g => g.Title.Contains("baby"))
                  .GroupJoin(ICDUnitOfWork.Alphas.Find()
                                          .Where(a => a.Title.Contains("baby"),
                             a => a.AlphaGroupID,
                             g => g.AlphaGroupID,
                             (alphaGroups, alphas) =>
                                new ICD.ViewModels.HomeSearchViewModel
                                 {
                                  AlphaGroups = alphaGroups,
                                  Alphas = alphas
                                 }).AsEnumerable();
于 2012-11-13T18:18:00.860 回答