C# Linq 子选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/527819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:51:20  来源:igfitidea点击:

Linq Sub-Select

c#linqouter-join

提问by Dan

How do I write a sub-select in LINQ.

如何在 LINQ 中编写子选择。

If I have a list of customers and a list of orders I want all the customers that have no orders.

如果我有一个客户列表和一个订单列表,我想要所有没有订单的客户。

This is my pseudo code attempt:

这是我的伪代码尝试:

    var  res = from c in customers 
where c.CustomerID ! in (from o in orders select o.CustomerID) 
select c

采纳答案by Jon Skeet

How about:

怎么样:

var res = from c in customers 
          where !orders.Select(o => o.CustomerID).Contains(c.CustomerID)
          select c;

Another option is to use:

另一种选择是使用:

var res = from c in customers
          join o in orders 
               on c.CustomerID equals o.customerID 
               into customerOrders
          where customerOrders.Count() == 0
          select c;

Are you using LINQ to SQL or something else, btw? Different flavours may have different "best" ways of doing it

你是使用 LINQ to SQL 还是其他什么东西,顺便说一句?不同的口味可能有不同的“最佳”方式

回答by Marc Gravell

If this is database-backed, try using navigation properties (if you have them defined):

如果这是数据库支持的,请尝试使用导航属性(如果您定义了它们):

var res = from c in customers
          where !c.Orders.Any()
          select c;

On Northwind, this generates the TSQL:

在 Northwind 上,这会生成 TSQL:

SELECT /* columns snipped */
FROM [dbo].[Customers] AS [t0]
WHERE NOT (EXISTS(
    SELECT NULL AS [EMPTY]
    FROM [dbo].[Orders] AS [t1]
    WHERE [t1].[CustomerID] = [t0].[CustomerID]
    ))

Which does the job quite well.

这可以很好地完成工作。

回答by omoto

var  res = (from c in orders where c.CustomerID == null
               select c.Customers).ToList();

or Make Except()

或 Make except()

回答by amit

            var result = (from planinfo in db.mst_pointplan_info
                                                           join entityType in db.mst_entity_type
                                                           on planinfo.entityId equals entityType.id
                                                           where planinfo.entityId == entityId
                                                           && planinfo.is_deleted != true
                                                           && planinfo.system_id == systemId
                                                           && entityType.enity_enum_id == entityId
                                                           group planinfo by planinfo.package_id into gplan
                                                           select new PackagePointRangeConfigurationResult
                                                           {
                                                               Result = (from planinfo in gplan
                                                                         select new PackagePointRangeResult
                                                                         {
                                                                             PackageId = planinfo.package_id,
                                                                             PointPlanInfo = (from pointPlanInfo in gplan
                                                                                              select new PointPlanInfo
                                                                                              {
                                                                                                  StartRange = planinfo.start_range,
                                                                                                  EndRange = planinfo.end_range,
                                                                                                  IsDiscountAndChargeInPer = planinfo.is_discount_and_charge_in_per,
                                                                                                  Discount = planinfo.discount,
                                                                                                  ServiceCharge = planinfo.servicecharge,
                                                                                                  AtonMerchantShare = planinfo.aton_merchant_share,
                                                                                                  CommunityShare = planinfo.community_share
                                                                                              }).ToList()
                                                                         }).ToList()
                                                           }).FirstOrDefault();