Filtering IQueryable sub list

前端 未结 4 2052
名媛妹妹
名媛妹妹 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:46

    I think what you are looking is SelectMany. As an example for your case is something like this:

      positiveItems = items.SelectMany(x => x.SubItem).Where(x=> x.ID == 1).Select(x=>x.Item);
      //items still IQueryable , so we can concat it with another IQueryable
    
      negativeItems = items.SelectMany(x=>x.SubItem).Where(x=>x.ID != 1).Select(x=>x.Item);
    
    
      //just an using option
      allItems = positiveItems.Concat(negativeItems);
    

    And just a suggestion. For high number of reference object set, you can use ValueInjecter It is very simple and fast. I used it couple of production projects and it saved my tons of times.

提交回复
热议问题