繁体   English   中英

如何选择具有分组LINQ查询的联接字段

[英]How to select joined fields with grouped LINQ query

我已经搜索了网站并切割并切割了几种不同的方式,但我是LINQ的新手,我无法弄清楚如何从我的联合表中选择字段。 每次我运行这个,我收到一条消息:

“列UPC不属于表格。”

“列条目不属于表格。”

audits表的每个文件名都有一行数据,scanDetail表有多个与每个文件名相关联的行。 我需要加入表格,对数据进行分组,然后选择不同数量的UPC,并且只显示最大条目数,这样每个文件名都有一行数据。

var query = from audit in audits.AsEnumerable()                
            join scan in scanDetail.AsEnumerable()
            on audit.Field<string>("filename") equals scan.Field<string>("filename")
            group audit by audit.Field<string>("filename") into g
            select new
            {
               Account = g.Select(x => x.Field<string>("Account")),
               Store = g.Select(x => x.Field<string>("Store")),
               AuditDate = g.Select(x => x.Field<string>("AuditDate")),
               UPCs = g.Select(x => x.Field<string>("UPC").Distinct().Count()),
               Qty = g.Select(x => x.Field<string>("ScanQty")),
               Retail = g.Select(x => x.Field<string>("RegTotal")),
               Entries = g.Select(x => x.Field<string>("Entry").Max()),
               Supervisor = g.Select(x => x.Field<string>("AuditSup")),
               Division = g.Select(x => x.Field<string>("StoreDivision")),
               Invoice = g.Select(x => x.Field<string>("InvAmount"))
           };

我用这种方式尝试了同样的结果。

  var query = 
           from audit in audits.AsEnumerable()  

           join scan in scanDetail.AsEnumerable()
           on audit.Field<string>("filename") equals
            scan.Field<string>("filename")
           group audit by new {storeDisk = audit.Field<string>("filename"), 
                               Account = audit.Field<string>("Account"),
                               Store = audit.Field<string>("Store"),
                               AuditDate = audit.Field<string>("AuditDate"),
                               UPCs = (from UPC in scanDetail.AsEnumerable()
                                       select scan.Field<string>("UPC")),
                               Qty = audit.Field<string>("ScanQty"),
                               Retail = audit.Field<string>("RegTotal"),
                               Entries = (from Entry in scanDetail.AsEnumerable()
                                         select scan.Field<string>("Entry")),
                               Supervisor = audit.Field<string>("AuditSup"),
                               Division = audit.Field<string>("StoreDivision"),
                               Invoice = audit.Field<string>("InvAmount")
           } into g
           select new
           {

               Account = g.Key.Account,
               Store = g.Key.Store,
               AuditDate = g.Key.AuditDate,
               UPCs = g.Select(x => x.Field<string>("UPC").Distinct().Count()),
               Qty = g.Key.Qty,
               Retail = g.Key.Retail,
               Entries = g.Select(x => x.Field<string>("Entry").Max()),
               Supervisor = g.Key.Supervisor,
               Division = g.Key.Division,
               Invoice = g.Key.Invoice

           };

您基本上需要从audit中获取所需的所有内容并将其放入组的“密钥”中。

var query = from audit in audits.AsEnumerable()                
            join scan in scanDetail.AsEnumerable()
            on audit.filename equals scan.filename
            group audit by new { audit.Account, audit.Store, etc... } into g
            select new
            {
               g.Key.Account,
               g.Key.Store,
               // etc for the rest of the audit fields
               Entries = g.Max(x => x.Entry),
               UPCs = g.Select(x => x.UPC).Distinct().Count()),
           };

由于“审计表的每个文件名都有一行数据” ,因此无需对该表中的数据进行分组。 在这种情况下,您可以使用组连接 ,而不是常规连接 ,根据文档

根据键相等性关联两个序列的元素,并对结果进行分组。

换句话说,对于外部表的每个元素,您将从内部表中获取一相关元素。 然后,您可以在该集上执行不同的聚合。 请记住,该集合可以为空,并且在这种情况下,某些聚合会抛出异常。

话虽如此,查询可能是这样的:

var query = from audit in audits.AsEnumerable()                
            join scan in scanDetail.AsEnumerable()
            on audit.Field<string>("filename") equals scan.Field<string>("filename")
            into scanGroup
            select new
            {
               Account = audit.Field<string>("Account"),
               Store = audit.Field<string>("Store"),
               AuditDate = audit.Field<string>("AuditDate"),
               UPCs = scanGroup.Select(scan => scan.Field<string>("UPC")).Distinct().Count(),
               Qty = audit.Field<string>("ScanQty"),
               Retail = audit.Field<string>("RegTotal"),
               Entries = scanGroup.Select(scan => scan.Field<string>("Entry")).OrderByDescending(x => x).FirstOrDefault(),
               Supervisor = audit.Field<string>("AuditSup"),
               Division = audit.Field<string>("StoreDivision"),
               Invoice = audit.Field<string>("InvAmount")
           };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM