Linq Sub-Select

后端 未结 4 1374
-上瘾入骨i
-上瘾入骨i 2020-12-16 15:31

How do I write a sub-select in 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

相关标签:
4条回答
  • 2020-12-16 15:42
    var  res = (from c in orders where c.CustomerID == null
                   select c.Customers).ToList();
    

    or Make Except()

    0 讨论(0)
  • 2020-12-16 15:44

    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

    0 讨论(0)
  • 2020-12-16 16:03
                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();
    
    0 讨论(0)
  • 2020-12-16 16:04

    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:

    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.

    0 讨论(0)
提交回复
热议问题