Filtering IQueryable sub list

前端 未结 4 2087
名媛妹妹
名媛妹妹 2021-01-20 19:33

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?

4条回答
  •  既然无缘
    2021-01-20 19:51

    If you want to filter children down to where there's only one child per parent, you need to start with children, select their parents, and do not touch the parents' subitems:

    IQueryable childItems = context
        .SubItems.Include("Item")
        .Where(si => si.Id == 1 && si.Item.SomeAttr == someValue);
    //               ^^^^^^^^^^    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    //                   |                         |
    //                   |           Set a condition on the parent
    //  Set a condition on the child
    

    I assume that each sub-item has a link "pointing" back at its parent.

提交回复
热议问题