我试图拉一组记录,出现在一组数据集不止一次。我这里的问题是,我的查询没有考虑到从初始查询分组,基本上是拉所有的记录。我有一个从数据表中提取数据的初始查询,如下所示:
Dim eDTKBase = From eDTKData In dteDTK _
Select New With _
{ _
.eDTK_PDP_Code = eDTKData.Field(Of String)("PDP_CODE"), _`
.eDTK_PDP_Description = eDTKData.Field(Of String)("PDP_DESCRIPTION"), _
.eDTK_WB_Slave = eDTKData.Field(Of String)("WALLBOARD_OR_SLAVE"), _
.eDTK_Print_On_DAD = eDTKData.Field(Of String)("PRINT_ON_DAD"), _
.eDTK_PLI = eDTKData.Field(Of String)("COMPASS_PLI"), _
.eDTK_Dead = eDTKData.Field(Of String)("DEAD"), _
.eDTK_Instance = eDTKData.Field(Of String)("COUNTRY_INSTANCE") _
}
然后,我尝试仅过滤eDTK_PDP_Code字段计数> 1的记录,仅用于由eDTK_PLI, eDTK_PDP_Code字段分组的记录,使用以下LINQ查询:
Dim EDTKMultPDPtoPLI = From EDTK In eDTKBase _
Group By EDTK.eDTK_PLI, _
EDTK.eDTK_PDP_Code _
Into g = Group _
Where eDTK_PDP_Code.Count > 1 _
Select eDTK_PLI, _
eDTK_PDP_Code, _
eDTK_PDP_Code.Count
问题是它是从数据表中评估整个数据集,而不仅仅是分组记录。我哪里做错了?谢谢。
我做了一些实验,我想我找到了一个解决方案。下面是使用AdventureWorks的代码,但我认为它应该是有意义的。
from r in (
from s in SalesOrderHeaders
select s.SalesPersonID,s.CustomerID
where SalesPersonID isnot nothing)
group r by key = new with {r.SalesPersonID,r.CustomerID} into group
where group.count > 1
select key.SalesPersonID, key.CustomerID, group.count()
在此结果中,显示客户与给定销售人员的订单次数。
RepID CustID Count
280 1 4
283 2 8
275 3 6
277 3 6
275 4 6
277 4 2
281 5 8
剪剩下
希望有帮助
韦德