我已经搜索了网站,并通过几种不同的方式切片和切块,但我是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
};
答案 0 :(得分:0)
你基本上需要从你需要的audit
中取出所有内容并将其放入&#34;键&#34;小组。
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()),
};
答案 1 :(得分:0)
由于&#34;审核表的每个文件名都包含一行数据&#34; ,因此无需对该表中的数据进行分组。在这种情况下,您可以使用group join代替常规联接,而documentation则根据{{3}}:
根据键相等性将两个序列的元素相关联,并对结果进行分组。
换句话说,对于外部表的每个元素,您将从内部表中获得 set 相关元素。然后,您可以在该集上执行不同的聚合。请记住,该集合可以为空,并且在这种情况下,某些聚合会抛出异常。
话虽如此,查询可能是这样的:
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")
};