Domanda

Come faccio a scrivere un sub-select in LINQ.

Se ho un elenco di clienti e una lista di ordini che voglio a tutti i clienti che non hanno ordini.

Questo è il mio tentativo pseudo codice:

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

Soluzione

Come su:

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

Un'altra opzione è quella di utilizzare:

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

Si sta utilizzando LINQ to SQL o qualcos'altro, btw? Sapori diversi possono avere diversi "migliori" modi di farlo

Altri suggerimenti

Se questo è il database-backed, provare a utilizzare le proprietà di navigazione (se li avete definiti):

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

In Northwind, questo genera il 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]
    ))

Il che fa il lavoro abbastanza bene.

            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  res = (from c in orders where c.CustomerID == null
               select c.Customers).ToList();

O Fai Salvo ()

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top