2008-09-09 14 views
48

मैं इस पर थोड़ा फंस गया हूँ। मूल रूप से मैं एसक्यूएल करने के लिए LINQ में निम्नलिखित SQL क्वेरी की तरह कुछ करना चाहता हूँ:आप LINQ से SQL के साथ एक उप-क्वेरी कैसे प्रबंधित कर सकते हैं?

SELECT f.* 
FROM Foo f 
WHERE f.FooId IN (
    SELECT fb.FooId 
    FROM FooBar fb 
    WHERE fb.BarId = 1000 
) 

कोई मदद कृतज्ञता प्राप्त की जाएगी।

धन्यवाद।

उत्तर

55

this article पर एक नज़र डालें की कोशिश करो। असल में, यदि आप IN के बराबर प्राप्त करना चाहते हैं, तो आपको पहले एक आंतरिक क्वेरी बनाने की आवश्यकता है, और उसके बाद Contains() विधि का उपयोग करें। यहाँ अनुवाद पर मेरे प्रयास है:

var innerQuery = from fb in FoorBar where fb.BarId = 1000 select fb.FooId; 
var result = from f in Foo where innerQuery.Contains(f.FooId) select f;
+0

लिंक के लिए धन्यवाद - यही वही था जो मुझे चाहिए था। उनके उत्तरों के लिए हर किसी के लिए भी धन्यवाद। –

+2

आपके पास पहली क्वेरी के साथ शब्दकोश बनाने के साथ बेहतर प्रदर्शन हो सकता है, क्योंकि दूसरी क्वेरी में शामिल() कॉल को O (1) के विपरीत ओ (1) में किया जा सकता है। –

+7

डैरेन, LINQ से SQL को SQL क्वेरी में बदल दिया जाएगा। ऑब्जेक्ट संग्रह पर पुनरावृत्ति करते समय शब्दकोश उपयोगी होगा। – aku

2

का उपयोग करके देखें दो अलग-अलग चरणों:

// create a Dictionary/Set/Collection fids first 
var fids = (from fb in FooBar 
      where fb.BarID = 1000 
      select new { fooID = fb.FooID, barID = fb.BarID }) 
      .ToDictionary(x => x.fooID, x => x.barID); 

from f in Foo 
where fids.HasKey(f.FooId) 
select f 
3
from f in Foo 
    where f.FooID == 
     (
      FROM fb in FooBar 
      WHERE fb.BarID == 1000 
      select fb.FooID 

     ) 
    select f; 
+8

यह वास्तव में काम करता है बनाने के? मुझे विश्वास करने में परेशानी है ... –

0

इस

var fooids = from fb in foobar where fb.BarId=1000 select fb.fooID 
var ff = from f in foo where f.FooID = fooids select f 
80

जनरल रास्ता LINQ में में लागू करने के लिए लागू करने के लिए

var q = from t1 in table1 
     let t2s = from t2 in table2 
        where <Conditions for table2> 
        select t2.KeyField 
     where t2s.Contains(t1.KeyField) 
     select t1; 

जनरल रास्ता SQL करने के लिए LINQ में मौजूद है

var q = from t1 in table1 
     let t2s = from t2 in table2 
        where <Conditions for table2> 
        select t2.KeyField 
     where t2s.Any(t1.KeyField) 
     select t1; 
+0

त्वरित और बिंदु - महान उत्तर। भले ही एक अलग बयान में आंतरिक क्वेरी का निर्माण निश्चित रूप से स्पष्ट हो, भले ही यह इनलाइन कैसे करें। धन्यवाद! –

+0

@aku क्या यह विधि @ सैमुएल जैक की विधि के लिए बेहतर है? –

+0

यह विधि चेनिंग वाक्यविन्यास से कहीं अधिक पठनीय है। धन्यवाद। –

0
var foos = Foo.Where<br> 
(f => FooBar.Where(fb.BarId == 1000).Select(fb => fb.FooId).Contains(f.FooId)); 
1

SQL करने के लिए // एक शब्दकोश/सेट/संग्रह फ़िड्स बनाएं पहले

Find Other Artilces

var fids = (from fb in FooBar 
      where fb.BarID = 1000 
      select new { fooID = fb.FooID, barID = fb.BarID }) 
      .ToDictionary(x => x.fooID, x => x.barID); 

from f in Foo 
where fids.HasKey(f.FooId) 
select f 
0

// एक शब्दकोश/सेट/संग्रह fids पहले

Find Other Artilces

var fids = (from fb in FooBar where fb.BarID = 1000 select new { fooID = fb.FooID, barID = fb.BarID }) .ToDictionary(x => x.fooID, x => x.barID); 

from f in Foo where fids.HasKey(f.FooId) select f 
0
from f in foo 
where f.FooID equals model.FooBar.SingleOrDefault(fBar => fBar.barID = 1000).FooID 
select new 
{ 
f.Columns 
}; 
संबंधित मुद्दे