Entity Framework 等同于以下查询?

Entity Framework equivalent of the following query?

谁能告诉我如何编写等于此的 EF 查询:

SELECT OI.orderid, 
       OI.inventoryid, 
       VP.vendorid 
FROM   orders O 
       INNER JOIN orderitems OI 
               ON O.orderid = OI.orderid 
       INNER JOIN vendorparts VP 
               ON VP.inventoryid = OI.inventoryid 
WHERE  (SELECT Count(*) 
        FROM   vendorparts 
               INNER JOIN vendors 
                       ON vendorparts.vendorid = vendors.vendorid 
                          AND vendors.candropship = 1 
        WHERE  vendorparts.inventoryid = VP.inventoryid 
               AND vendorparts.vendorid IN ( 1, 17 )) > 1 

我正在尝试做这样的事情...

var q = from o in _context.Orders
            join oi in _context.Orderitems on o.Orderid equals oi.Orderid
            join vp in _context.Vendorparts on oi.Inventoryid equals  vp.Inventoryid
                where (from vp2 in _context.Vendorparts
                       where vp2.Inventoryid == vp.Inventoryid                           
                       select count(*)) > 1

但显然这在语法上是不允许的。

长答案:

var vendorIds = new int[] { 1, 17 };

_context
    .Orders
    .Join(_context.Orderitems,
        o => o.Orderid,
        oi => oi.Orderid,
        (o, oi) => new { o, oi })
    .Join(_context.Vendorparts,
        o_oi => o_oi.oi.Inventoryid,
        vp => vp.Inventoryid,
        (o_oi, vp) => new { o_oi.o, o_oi.oi, vp })
    .Where(o_oi_vp => _context.Vendorparts
        .Where(vp
            => vp.Inventoryid == o_oi_vp.vp.Inventoryid
            && vendorIds.Contains(vp.Vendorid))
        .Join(_context.Vendors.Where(v => v.Candropship),
            vp => vp.Vendorid,        
            v => v.Vendorid,
            (vp, v) => new { vp, v })
        .Any())
    .Select(o_oi_vp => new {
        o_oi_vp.oi.orderid, 
        o_oi_vp.oi.inventoryid, 
        o_oi_vp.vp.vendorid 
    });

简答:使用存储过程。