【问题标题】:Linq to SQl, select same column from multiple tablesLinq to SQl,从多个表中选择同一列
【发布时间】:2010-10-03 15:57:25
【问题描述】:

我一直在尝试开发一个 linq 查询,它返回数据库中所有表的 ItemNumber 列,但到目前为止我还没有成功。

基本上我对计算机中的每种硬件组件都有一个表,每个表都有一个 ItemNumber 列。我需要一口气查询所有表,并在平面列表/数组中返回 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 };

【问题讨论】:

    标签: c# .net linq linq-to-sql


    【解决方案1】:

    你所描述的是一个联合:

    SELECT ItemNumber FROM tbl1 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 执行单独的查询,因此如果您计划对大量数据执行复杂操作,您最好重新考虑您的数据库设计。

    【讨论】:

    • 我刚刚编辑了代码布局以使其更易于阅读。希望你不要介意。
    • 我想我不小心解开了那个!
    • @Jeff Opps 同时格式化了代码,我已经回滚到你的版本了。
    • 我实际上一直在重新思考我们的数据在数据库中的存储方式。由于所有表都具有相同的结构,我可以使用一个表并添加一个“类型”列。我决定使用当前方法,因为它允许我们在需要时为每个硬件组件添加列。非常感谢!
    【解决方案2】:

    不要使用 Union - 而是使用 Concat!

    • LinqToSql 的 Union 映射到 T-Sql 的 Union。
    • LinqToSql 的 Concat 映射到 T-Sql 的 Union All。

    不同之处在于 Union 要求列表相互检查并删除重复项。这种检查会花费时间,而且我希望您的零件编号是全球唯一的(仅出现在单个列表中)。 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();
    

    【讨论】:

    • 谢谢。我都想,'Donde esta la equivalente de Union All en Linq2SQL?'
    【解决方案3】:

    联合见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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-20
      • 1970-01-01
      • 2010-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多