我有以下C3查询,我需要将其转换为vb查询,但我无法让该组正常工作。 AutoComplete是一个包含(2)字段ACCode和ACName的类。
string[] CodesnotListed = { "F97", "F98", "F99" };
Model1 = from x in db.CIPCodes
where ((x.ClusterCode.StartsWith(filter) || x.ClusterName.StartsWith(filter)) &&
!CodesnotListed.Contains(x.ClusterCode))
orderby x.ClusterCode
group x by new AutoCompleteResults { ACCode = x.ClusterCode, ACName = x.ClusterName } into Alpha
select new AutoCompleteResults
{
ACCode = Alpha.Key.ACCode,
ACName = Alpha.Key.ACCode + " - " + Alpha.Key.ACName
};
这就是我所写的,但它通过指示它需要一个Type然后如果我指明了类然后它告诉我可以推断出Range而将它挂在Group上。
Dim CodesnotListed As String() = {"F97", "F98", "F99"}
Dim x = From y In _db.CIPCodes _
Where (y.ClusterCode.StartsWith(filter) OrElse y.ClusterName.StartsWith(filter)) _
AndAlso Not CodesnotListed.Contains(y.ClusterCode) AndAlso y.cipactive = True _
Order By y.ClusterCode
Group y By New (y.ClusterCode, y.ClusterName) Into g
Select New AutoCompleteResults With {
.ACCode = y.ClusterCode,
.ACName = y.ClusterCode + " - " + y.ClusterName}
Return x.ToList
感谢。
答案 0 :(得分:3)
VB中语法的正确组有点难以捉摸。以下是代码的工作版本:
Dim CodesnotListed As String() = {"F97", "F98", "F99"}
Dim x = (From y In _db.CIPCodes
Where (y.ClusterCode.StartsWith("") OrElse y.ClusterName.StartsWith("")) _
AndAlso Not CodesnotListed.Contains(y.ClusterCode) AndAlso y.cipactive
Order By y.ClusterCode
Group y By cc = y.ClusterCode, cn = y.ClusterName Into g = Group
Select New AutoCompleteResults With {.ACCode = cc, .ACName = cc & " - " & cn}).ToList()
Dave Doknjas指出,有一些错误。所以我重写了代码并将其放入单元测试项目中并通过了。新代码应该做你需要的一切。我还修改了一下,将结果数据集作为列表返回,而不是将其存储在临时变量x
中(如果您需要x
变量,我认为您可以轻松切换回来东西)。
答案 1 :(得分:0)
您对两个对象初始值设定项的处理方式不同 - 一致的转换是:
Dim CodesnotListed() As String = { "F97", "F98", "F99" }
Model1 = From x In db.CIPCodes
Where ((x.ClusterCode.StartsWith(filter) OrElse x.ClusterName.StartsWith(filter)) AndAlso Not CodesnotListed.Contains(x.ClusterCode))
Order By x.ClusterCode
Group x By GroupKey = New AutoCompleteResults With {
.ACCode = x.ClusterCode,
.ACName = x.ClusterName
} Into Alpha = Group
Select New AutoCompleteResults With {
.ACCode = GroupKey.ACCode,
.ACName = GroupKey.ACCode & " - " & GroupKey.ACName
}
可能有更好的方法来构造查询,但这与原始C#一致。