C#按问题分组

C#按问题分组,c#,vb.net,linq,C#,Vb.net,Linq,我有以下C3查询,我需要将其转换为vb查询,但我无法使group by正常工作。AutoComplete是一个包含(2)个字段ACCode和ACName的类 string[] CodesnotListed = { "F97", "F98", "F99" }; Model1 = from x in db.CIPCodes where ((x.ClusterCode.StartsWith(filter) || x.ClusterName.StartsWith(filter)

我有以下C3查询,我需要将其转换为vb查询,但我无法使group by正常工作。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
         };
这是我写的,但它挂起了组,因为它指示它需要一个类型,然后如果我指示类,它告诉我可以推断范围

    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

谢谢

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
变量,您可以轻松切换回该变量)。

VB中正确的group by语法有点难以捉摸。以下是代码的工作版本:

    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
变量进行某些操作,您可以轻松切换回该变量)。

您对两个对象初始值设定项的处理方式不同-一致的转换是:

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#是一致的。

您对两个对象初始值设定项的处理方式不同-一致的转换是:

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#是一致的。

为什么在VB版本中添加AndAlso y.cipactive=True?为什么在VB版本中添加AndAlso y.cipactive=True?您的“y组按新(y.ClusterCode,y.ClusterName)”不会编译。@DaveDoknjas啊,是的,我现在明白了。我们可以解决这个问题。@DaveDoknjas好的,现在看看,让我知道你的想法。我通过一个单元测试项目验证了它,以确保没有任何明显的错误。如果它起作用,你不能对此提出异议-对象初始值设定项的处理方式有所不同,但不管怎样,这可能是对原始代码的一种改进。你的“y组新代码(y.ClusterCode,y.ClusterName)”将无法编译。@DaveDoknjas啊,是的,我现在明白了。我们可以解决这个问题。@DaveDoknjas好的,现在看看,让我知道你的想法。我通过一个单元测试项目验证了它,以确保没有任何明显的bug。如果它起作用,你不能对此提出异议-对象初始值设定项的处理方式不同,但无论如何,这可能是对原始代码的改进。