“分组依据”条款不起作用

时间:2018-05-24 10:08:18

标签: c# sql visual-studio group-by

我把你放在上下文中,我们正在用.NET和C#做一个项目。 该项目链接到以下SQL数据库表。

enter image description here

在项目中,我们有一个搜索字段,用户可以在其中键入,我们在数据库中使用 PROTOTYPE_CODE 字段过滤 textSearch

问题是我们达到了这样的程度:当用户输入时,例如 P30 ,软件会正确过滤它,并且只显示带有“P30”的行作为代码:

enter image description here

现在,当我们遇到问题时,我们希望将具有相同代码(PROTOTYPE_CODE)的行分组以获得类似的响应,而不是使用“P30”的两行,只需获得一行“P30”但总和列上的数字。在这种情况下会是这样的:

enter image description here

由于VS给我们带来了常见的错误,我们无法做到这一点。我向您展示了我们尝试的不同方式。

 private IEnumerable<Prototypes> GetQuery()
        {
            return from search in prototypesBindingSource.DataSource as List<Prototypes>
                   where search.PROTOTYPE_CODE == (textSearch.Text) || search.PROTOTYPE_DESCR == (textSearch.Text) 
                   group search by search.PROTOTYPE_CODE into codeGroup
                   select search;
        }

在这种情况下,我们得到“当前上下文中不存在名称'搜索'”错误“选择搜索。

我们也试过

Group By codice = search.PROTOTYPE_CODE
Into Classes = Group, Count();

获得相同的错误。我们用不同的方式尝试了它,从来没有得到这个计数的预期结果,因为我们真的被困在这里,找到正确的方法会很棒。谢谢!

1 个答案:

答案 0 :(得分:2)

您的select语句应如下所示:

return from search in prototypesBindingSource.DataSource as List<Prototypes>
                   where search.PROTOTYPE_CODE == (textSearch.Text) || search.PROTOTYPE_DESCR == (textSearch.Text) 
                   group search by search.PROTOTYPE_CODE into codeGroup
                   select new {
    Code = codeGroup.Key,
    Count = codeGroup.Count()
};
相关问题