过滤IQueryable子列表 [英] Filtering IQueryable sub list

查看:96
本文介绍了过滤IQueryable子列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用实体框架,但这可能是不相关的
如果我有一个Iqueryable,我如何筛选一个子列表并保持它IQueryable,所以它还没有打到DB?

Working with Entity Framework, but that's probably irrelevant If I have an Iqueryable, how do I filter a sub list and keep it IQueryable so it doesn't yet hit the DB?

如果我有10个项目,每个都有3个子项目,那么我该如何进行过滤呢?所有10个项目都有一个返回,它们的子项被过滤到id = 1?

If I have 10 items and each has 3 sub items, how do I filter it so there's a return of all 10 items and their subitems are filtered to where id = 1?

class Item有大约20个属性,所以我不想使用每个属性的投影,因为维护方面的问题。

class Item has about 20 properties on it, so I wouldn't want to use projection on each of them, because of maintenance concerns..

items = items.select(??);//how to do this so items are returned, and their children are filtered?

class SubItem
{ private int ID { get; set; }
}
class Item 
{
private List<SubItem> SubItems { get; set; }
}


推荐答案

您要返回所有项目,无论什么,但您想要过滤 SubItems 。没有好的方法可以说我想要返回这个对象,除了我想要一个修改版本的X属性一个 IQueryable 。您必须使用select语句,如果需要,您可以选择一个新的对象。

I interpret your question as you want to return all Items no matter what, but you want to filter SubItems. There is no good way to say "I want to return this object except I want a modified version of X property" for an IQueryable. You'll have to use a select statement where you select a new object if you want to this.

选项1:单独返回数据

var itemsAndSubItems = items
    .Select(item => new 
        {
            Item = item,
            SubItems = item.SubItems.Where(sub => sub.ID = 1)
        }
    );

或者如果您不介意热切地将项目加载到内存中:

or if you don't mind eagerly loading the items into memory:

IEnumerable<Item> = items
    .Select(item => new 
        {
            Item = item,
            SubItems = item.SubItems.Where(sub => sub.ID = 1)
        }
    )
    .ToList()
    .Select(row => 
        { 
            var item = row.Item;
            item.SubItems = row.SubItems;
            return item;
        }
    );

选项2:返回一个新的类的实例(看起来你不想做的) )

Option 2: Return a new instance of your class (which it seems you don't want to do)

IQueryable<Item> items = items
    .Select(item => new Item 
        { 
            SubItems = item.SubItems.Where(sub => sub.ID == 1),
            OtherProp = item.OtherProp
            /*etc for the other properties on Item*/
        }
    );

选项3:向您的课堂添加另一个属性。我推荐这个最少。请注意,当您访问SubItemsWithIdOne

Option 3: Add another property to your class. I recommend this least. Note that your query will still return all sub items here when you access SubItemsWithIdOne

class Item 
{
    private List<SubItem> SubItems { get; set; }
    private List<SubItem> SubItemsWithIdOne 
    {
        get 
        {
            return this.SubItems.Where(sub => sub.ID == 1); 
        }
    }
}

选项4:添加属性在 SubItem 引用其父。然后返回一个 SubItem 的列表。这样你就可以满足 SubItems 项目。您的条件满足

Option 4: Add a property on SubItem that references it's parent Item. Then return a list of SubItem. This way you'll have both SubItems and Items where your criteria is satisfied.

...如果您正在使用 IEnumerable ,则可以执行以下操作:

...If you're working with an IEnumerable you can do:

IEnumerable items = items
    .Select(item =>
        {
            item.SubItems.Where(sub => sub.ID = 1);
            return item;
        }
    );

这篇关于过滤IQueryable子列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆