通过LINQ SQL查询将List <string>嵌套在自定义类列表中

时间:2016-07-21 03:24:16

标签: c# linq linq-to-sql

这是我的代码,它从数据库中检索有关打印机驱动程序列表的信息。该表包含打印机驱动程序列表,以及它们所在的服务器。

 public List<PrinterDrivers> GetPrinterDriversFromCache()
        {
            using (dbPrintSimpleDataContext db = new dbPrintSimpleDataContext())
            {
                var q = from p in db.GetTable<tblPrinterDriverCache>()
                        where p.CacheGUID == mostRecentCacheID()
                        group p by p.PrinterDriver into g
                        select new PrinterDrivers
                        {
                            DriverName = g.Key,
                            InstalledOn = g.Where(x => x.PrinterDriver == g.Key).Select(x => x.PrinterServer).ToList(),
                            Usable = (g.Count() == Properties.Settings.Default.PrintServers.Count)
                        };
                return q.ToList();
            }
        }

我想要返回的是一个List,其中包含一个属性,其中包含一个List,其中包含打印机驱动程序所在的服务器。我认为我违背了当前LINQ SQL知识的限制:(

结果列表应包含:

DriverName =打印机驱动程序名称,在本例中为组密钥(字符串)

InstalledOn = List(包含找到此打印机驱动程序的服务器列表)

Usable =一个简单的bool检查它找到的服务器是否与我们在首选项文件中的服务器数量相同。

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

试试这个:

LINQ Lambda, Group by with list

问题在于Linq不了解$(window).scroll(function() { // selectors var $window = $(window), $body = $('body'), $panel = $('.section'); var scroll = $window.scrollTop(); $panel.each(function () { var $this = $(this); // if position is within range of this panel. // So position of (position of top of div <= scroll position) && (position of bottom of div > scroll position). // Remember we set the scroll to 33% earlier in scroll var. if ($this.position().top <= scroll && $this.position().top + $this.height() > scroll) { // Remove all classes on body with color- $body.removeClass(function (index, css) { return (css.match (/(^|\s)color-\S+/g) || []).join(' '); }); $('.section').removeClass('active'); $this.addClass('active'); } }); }).scroll(); 。只有整个查询的一部分在服务器上执行,因为在最终的ToList电话之前有一个额外的ToList电话(下面未经测试的代码)

ToList

从我链接的答案翻译相同的模式,你的将是:

public List<PrinterDrivers> GetPrinterDriversFromCache()
    {
        using (dbPrintSimpleDataContext db = new dbPrintSimpleDataContext())
        {
            var q = (from p in db.GetTable<tblPrinterDriverCache>()
                    where p.CacheGUID == mostRecentCacheID()
                    group p by p.PrinterDriver.DriverName into g
                    select g
                    ).ToList().Select(g => new PrinterDrivers
                    {
                        DriverName = g.Key,
                        InstalledOn = g.Where(x => x.PrinterDriver == g.Key).Select(x => x.PrinterServer).ToList(),
                        Usable = (g.Count() == Properties.Settings.Default.PrintServers.Count)
                    });
            return q.ToList();
        }
    }