为什么不从此linq查询中删除子项?

问题描述 投票:1回答:1

我有这个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()

时出错

我有这个linq查询,它检索数据库中的所有元素,并根据某些条件对输出进行过滤。我希望获得某些实体的列表,并且仅具有符合特定条件的属性...

c# linq
1个回答
0
投票

您的属性都包括在内,因为您正在选择具有任何已过滤属性的父级(实体)。您不会在初始查询中过滤掉属性本身。

© www.soinside.com 2019 - 2024. All rights reserved.