Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq子选择_C#_Linq_Outer Join - Fatal编程技术网

C# Linq子选择

C# Linq子选择,c#,linq,outer-join,C#,Linq,Outer Join,如何在LINQ中编写子选择 如果我有一个客户列表和一个订单列表,我想要所有没有订单的客户 这是我的伪代码尝试: var res = from c in customers where c.CustomerID ! in (from o in orders select o.CustomerID) select c 如果这是数据库支持的,请尝试使用导航属性(如果已定义它们): 在Northwind上,这将生成TSQL: SELECT /* columns snipped */ FR

如何在LINQ中编写子选择

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

这是我的伪代码尝试:

    var  res = from c in customers 
where c.CustomerID ! in (from o in orders select o.CustomerID) 
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]
    ))
这项工作做得很好

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

怎么样:

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;

顺便说一句,您是在使用LINQtoSQL还是其他什么?不同的口味可能有不同的“最佳”方式

在可读性方面,使用Any()代替Count()不是稍微好一点吗?我在读比尔·瓦格纳的《更有效的C#》,这是其中的一个建议。是的,很有可能。有很多方法。可以说,如果有一个空()或None()扩展方法,这也是任何()的反面,那该多好啊。。。
            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();
            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();