我正在尝试将一些代码从VB转换为C#,并且使用这个特定的LINQ查询时非常困难。首先,我将展示课程:
Public Class CompanyData
Property CompanyID As String = ""
Property VendorNumber As String = ""
Property VendorName As String = ""
End Class
Public Class MultiCompData
Inherits CompanyData
Property ExpCompanyId As String = ""
End Class
然后在VB示例中它所做的是将all all分组到multiCompGroup字段中的MultiCompData,foreach通过它并在ExpCompanyId为null / empty时添加到另一个CompanyData列表。代码:
Sub ProcessMultiCompData(multiCompDataJEs As IEnumerable(Of MultiCompData), ByRef multiCompData As List(Of CompanyData), ByRef undeterminedDists As List(Of CompanyData))
Dim multiCompGroups =
From m In multiCompDataJEs
Group m By m.ExpCompanyId,
m.VendorNumber,
m.VendorName
Into Group
For Each grp In multiCompGroups
If grp.ExpCompanyId = "" Then
undeterminedDists.AddRange(grp.Group)
在foreach中,grp具有以下属性 - ExpCompanyId,VendorNumber,VendorName和Group(这是我无法在C#中复制的部分)组是IEnumerable。是不是可以将C#中的IEnumerable添加到multiCompGroups?我试过了:
var multiCompGroups = from m in multiCompDataJEs group by new {m.ExpCompanyiD, m.VendorNumber, m.VendorName} into multiGroups
当我到达foreach时,multiGroups并没有显示为我的grp值中的IEnumerable,我也是这样尝试的:
var multiCompGroups = multiCompDataJEs.GroupBy(x => new {m.ExpCompanyId, m.VendorNumber, m.VendorName}).Select(y => new {y.Key.ExpCompanyId, y.Key.VendorNumber, y.Key.VendorName}).ToList();
两者都在生产同样的东西。当我做grp时,我有3个属性出现。但是"进入组" (上面VB代码中的grp.Group)没有出现在C#中。它甚至可以吗?
答案 0 :(得分:2)
您正在寻找的C#是:
var multiCompGroups =
from m in multiCompDataJEs
group m by new {m.ExpCompanyiD, m.VendorNumber, m.VendorName} into multiGroups
select multiGroups;
foreach(var grp in multiCompGroups)
{
if(grp.Key.ExpCompanyId == "")
{
undeterminedDists.AddRange(grp);
}
}