首页 > 解决方案 > 为什么 Any 不从此 linq 查询中删除子项?

问题描述

我有这个 linq 查询,它检索数据库中的所有元素,并根据某些条件过滤输出..

我想要一个特定实体的列表,只有符合特定过滤器的属性

    var entitiesWithLookupsTolookupEntityName = schemaRepository
        .Many()
        .OrderByDescending(x => x.Version)
        .Take(1)
        .Include(x => x.Entities)
        .ThenInclude(x => x.Attributes)
        .ThenInclude(x => x.AttributeTypeSpecification)
        .SelectMany(x => x.Entities
            .Where(x => x.Attributes.Any(y => y.Type == DataType.Lookup && y.AttributeTypeSpecification.EntityInternalName == lookupEntityName))).AsEnumerable();

这将返回唯一符合过滤器的实体,但还包括不符合过滤器的实体的所有属性。

我可以在像这样的第二个查询中将它们过滤掉

        var attributefiltered = entitiesWithLookupsTolookupEntityName.SelectMany(x =>
            x.Attributes.Where(y =>
                y.Type == DataType.Lookup && y.AttributeTypeSpecification.EntityInternalName == lookupEntityName));

但是为什么我不能将这两者结合起来呢?

似乎很奇怪,我能够做到两步但不能一步?使用 .where() .any() 有问题

标签: c#entity-frameworklinq

解决方案


推荐阅读