2009-01-26 102 views
2

我一直在試圖開發一個linq查詢,它返回數據庫中所有表的ItemNumber列,但到目前爲止我還沒有能夠成功完成。Linq到SQl,從多個表中選擇相同的列

基本上我有一個計算機中每種硬件組件的表,每個表都有一個ItemNumber列。我需要在一個bang中查詢所有表,然後返回一個扁平列表/數組中的ItemNumber值。 (基本上我希望能夠做到以下)

foreach (var c in items) { 
       Console.WriteLine(c.ItemNumber); 
      } 

搜索網無濟於事,可能有人告訴我如何做到這一點的例子嗎?我最好的嘗試如下,但我不明白Sql足以實現這一點。

var items = from hc in dc.DataBoxPCHardwareCases 
         from hhd in dc.DataBoxPCHardwareHardDrives 
         from hkb in dc.DataBoxPCHardwareKeyboards 
         from hmm in dc.DataBoxPCHardwareMemories 
         from hmo in dc.DataBoxPCHardwareMonitors 
         from hmb in dc.DataBoxPCHardwareMotherboards 
         from hms in dc.DataBoxPCHardwareMouses 
         from hod in dc.DataBoxPCHardwareOpticalDrives 
         from hps in dc.DataBoxPCHardwarePowerSupplies 
         from hpc in dc.DataBoxPCHardwareProcessors 
         from hsp in dc.DataBoxPCHardwareSpeakers 
         from hvc in dc.DataBoxPCHardwareVideoCards 
         from sos in dc.DataBoxPCSoftwareOperatingSystems 
         select new { hc, hhd, hkb, hmm, hmo, hmb, hms, hod, hps, hpc, hsp, hvc, sos }; 

回答

5

你所描述的是一個聯盟:

SELECT ItemNumber FROM TBL 1 UNION SELECT ItemNumber FROM tbl2的

在LINQ:

var itemCounts = (from hhd in dc.HHD select hhd.ItemNumber) 
       .Union((from hkb in dc.HKB select hkb.ItemNumber) 
         .Union(from hmm in dc.HMM select hmm.ItemNumber)) 
       and so on 

注意,使用的UNION這樣不非常有效。您可以在數據庫中進行一次往返訪問,但數據庫服務器必須爲每個UNION執行一次單獨的查詢,因此如果您計劃對大量數據執行復雜的操作,那麼重新考慮可能會更好一些你的數據庫設計。

+0

我剛編輯了代碼佈局,使其更易於閱讀。希望你不介意。 – 2009-01-26 23:19:37

+0

我想我意外地解開了! – 2009-01-26 23:20:47

1

聯盟見here

var items = (from hc in dc.DataBoxPCHardwareCases select hc.ItemNumber).Union 
      (from hhd in dc.DataBoxPCHardwareHardDrives select hkd.ItemNumber).Union 
      (from hkb in dc.DataBoxPCHardwareKeyboards select hkb.ItemNumber).Union 
      (from hmm in dc.DataBoxPCHardwareMemories select hhh.ItemNumber).Union 
      (from hmo in dc.DataBoxPCHardwareMonitors select hmo.ItemNumber).Union 
      (from hmb in dc.DataBoxPCHardwareMotherboards select hmb.ItemNumber).Union 
      (from hms in dc.DataBoxPCHardwareMouses select hms.ItemNumber).Union 
      (from hod in dc.DataBoxPCHardwareOpticalDrives select hod.ItemNumber).Union 
      (from hps in dc.DataBoxPCHardwarePowerSupplies select hps.ItemNumber).Union 
      (from hpc in dc.DataBoxPCHardwareProcessors select hpc.ItemNumber).Union 
      (from hsp in dc.DataBoxPCHardwareSpeakers select hsp.ItemNumber).Union 
      (from hvc in dc.DataBoxPCHardwareVideoCards select hvc.ItemNumber).Union 
      (from sos in dc.DataBoxPCSoftwareOperatingSystems select sos.ItemNumber) 
5

不要使用聯盟 - 改用Concat

  • LinqToSql的聯合被映射到T-SQL的聯合
  • LinqToSql的Concat被映射到T-Sql的Union All

所不同的是,聯盟要求列表相互檢查並刪除重複項。這種檢查費用的時間,我希望你的零件號碼是全球唯一的(只出現在一個單一的列表)無論如何。 聯盟全部跳過此額外檢查和重複刪除。

List<string> itemNumbers = 
    dc.DataBoxPCHardwareCases.Select(hc => hc.ItemNumber) 
    .Concat(dc.DataBoxPCHardwareHardDrives.Select(hkd => hkd.ItemNumber)) 
    .Concat(dc.DataBoxPCHardwareKeyboards.Select(hkb => hkb.ItemNumber)) 
    .Concat(dc.DataBoxPCHardwareMemories.Select(hhh => hhh.ItemNumber)) 
    .Concat(dc.DataBoxPCHardwareMonitors.Select(hmo => hmo.ItemNumber)) 
    .Concat(dc.DataBoxPCHardwareMotherboards.Select(hmb => hmb.ItemNumber)) 
    .Concat(dc.DataBoxPCHardwareMouses.Select(hms => hms.ItemNumber)) 
    .Concat(dc.DataBoxPCHardwareOpticalDrives.Select(hod => hod.ItemNumber)) 
    .Concat(dc.DataBoxPCHardwarePowerSupplies.Select(hps => hps.ItemNumber)) 
    .Concat(dc.DataBoxPCHardwareProcessors.Select(hpc => hpc.ItemNumber)) 
    .Concat(dc.DataBoxPCHardwareSpeakers.Select(hsp => hsp.ItemNumber)) 
    .Concat(dc.DataBoxPCHardwareVideoCards.Select(hvc => hvc.ItemNumber)) 
    .Concat(dc.DataBoxPCSoftwareOperatingSystems.Select(sos => sos.ItemNumber)) 
    .ToList(); 
相關問題