0

これが私のSQLクエリです:
これをもっときれいなものに変換するのを手伝ってくれませんか??

SELECT [PurchaseRequestID], [ProjectID],[FullName]
FROM PurchaseRequest
  WHERE [PurchaseRequestID] IN
    (SELECT [PurchaseRequestID] FROM PurchaseRequestDetail )
    AND [PurchaseRequestID] NOT IN
      (SELECT [PurchaseRequestID] FROM [PurchaseOrder] )

私はすでにこれを成功裏に変換しましたが、これは読み取り可能ではなく、書き直す必要があると思います。

var query = from a in db.PurchaseRequests
where
   (from b in db.PurchaseRequestDetails
    select new
    {
        b.PurchaseRequestID
    }).Contains(new { a.PurchaseRequestID }) &&
!(from c in db.PurchaseOrders
    select new
    {
        c.PurchaseRequestID
    }).Contains(new { a.PurchaseRequestID })
select a;

ありがとう

4

3 に答える 3

2

これらすべての匿名オブジェクトは本当に必要ありません。サブクエリで直接操作を行う代わりに、 letキーワードを使用して一時変数を導入します。

from a in db.PurchaseRequests
let b = from b in db.PurchaseRequestDetails select b.PurchaseRequestID
let c = from c in db.PurchaseOrders select c.PurchaseRequestID
where b.Contains(a.PurchaseRequestID) && !c.contains(a.PurchaseRequestID)
select a;
于 2012-04-09T12:56:50.147 に答える
1
var query = from a in db.PurchaseRequests
where
   db.PurchaseRequestDetails.Any(x => x.PurchaseRequestID == a.PurchaseRequestID) &&
   !db.PurchaseOrders.Any(x => x.PurchaseRequestID == a.PurchaseRequestID)
select a;
于 2012-04-09T12:56:51.100 に答える
0

ナビゲーション プロパティが設定されている場合は、次のようにクエリを記述できます。

IQueryable<PurchaseRequest> query = 
  from purchaseRequest in myDataContext.PurchaseRequests
  where purchaseRequest.PurchaseRequestDetail.Any()
  where !purchaseRequest.PurchaseOrder.Any()
  select purchaseRequest;

または、必要に応じてこのラムダ/メソッド スタイル...

IQueryable<PurchaseRequest> query2 = myDataContext.PurchaseRequests
  .Where(purchaseRequest => purchaseRequest.PurchaseRequestDetail.Any())
  .Where(purchaseRequest => !purchaseRequest.PurchaseOrder.Any());
于 2012-04-10T06:03:07.733 に答える