简体   繁体   English

从另一个查询中的一个查询中排除LINQ结果

[英]Exclude LINQ results from one query in another LINQ query

I have two linq queries, querying two different entities. 我有两个linq查询,查询两个不同的实体。 One entity contains all the warehouses, and another contains warehouses I don't need. 一个实体包含所有仓库,另一个包含我不需要的仓库。

I use this query to get all the warehouses I don't need: 我使用此查询来获取所有不需要的仓库:

var sysproWarehouses = from i in sysproSession.Query<InvWarehouse>()
                            group i by i.Warehouse
                            into g
                            select new
                            {
                                g.Key 
                            };

This is the query where I want to get all the warehouses I do need: 这是我要获取我需要的所有仓库的查询:

var stockEvaluation = from ib in mapicsSession.Query<ItemBalance>()
                                  where //I guess it needs to be done here
                                  orderby w.Description
                                  group ib by w.Description
                                  into g
                                  select new
                                  {
                                      Warehouse = g.Key,
                                  };

Basically I just need to exclude the first query results from the second query. 基本上,我只需要从第二个查询中排除第一个查询结果。 I apologise if this is a simple question, but I am a beginner so... Thank you! 抱歉,这是一个简单的问题,但是我是初学者,所以...谢谢!

Here is what you can do: 您可以执行以下操作:

var sysproWarehouses = from i in sysproSession.Query<InvWarehouse>()
                            group i by i.Warehouse
                            into g
                            select new
                            {
                                g.Key 
                            };

var stockEvaluation = from ib in mapicsSession.Query<ItemBalance>()
                        orderby w.Description
                        group ib by w.Description
                        into g
                        select new
                        {
                            g.Key,
                        };

Now, exclude the sysproWarehouses list items from stockEvaluation list: 现在,从stockEvaluation列表中排除sysproWarehouses列表项:

var result = stockEvaluation.Except(sysproWarehouses);

Note:- result contains the excluded items 注意:-结果包含排除的项目

I suppose you need an Except , but after the call to Select , as your two lists come from different tables. 我想您需要一个Except ,但是调用Select ,因为您的两个列表来自不同的表。 Something like this: 像这样:

var stockEvaluation = (from ib in mapicsSession.Query<ItemBalance>()
                      orderby w.Description
                      group ib by w.Description
                      into g
                      select new
                      {
                          g.Key,
                      }).Except(sysproWarehouses);

Be aware that I also changed the only member within your anonymous type to match the type from your first query. 请注意,我还更改了匿名类型中的唯一成员,以匹配您的第一个查询中的类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM