【问题标题】:Linq Sub-SelectLinq 子选择
【发布时间】:2010-10-06 09:11:25
【问题描述】:

如何在 LINQ 中编写子选择。

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

这是我的伪代码尝试:

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

【问题讨论】:

    标签: c# linq outer-join


    【解决方案1】:

    怎么样:

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

    另一种选择是使用:

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

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

    【讨论】:

    • 在可读性方面使用 Any() 代替 Count() 不是更好吗?正在阅读 Bill Wagner 的《更有效的 C#》,这是其中的一项建议。
    • 是的,很有可能。很多方法可以做到。可以说如果有一个与 Any() 相反的 Empty() 或 None() 扩展方法会很好......
    【解决方案2】:

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

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

    在 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]
        ))
    

    哪个做得很好。

    【讨论】:

      【解决方案3】:
                  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();
      

      【讨论】:

        【解决方案4】:
        var  res = (from c in orders where c.CustomerID == null
                       select c.Customers).ToList();
        

        或 Make except()

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-01-12
          • 1970-01-01
          • 1970-01-01
          • 2023-03-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-14
          相关资源
          最近更新 更多