VB.NET LINQ群组由多列组成 [英] VB.NET LINQ Group By Multiple Columns

查看:166
本文介绍了VB.NET LINQ群组由多列组成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下LINQ代码,它有一个语法错误,我不知道如何解决它

I have the following LINQ code which has a syntax error and i've no idea how to fix it

         Dim query =
        From row In mainDatatable.AsEnumerable

    'the syntax Error is in the following line
      Group row By new  { row.Field(Of Double)("cprice") , row.Field(Of Integer)("ccategory")} 
Into ProductGroups
    '**************************

                Dim grpNumber = 1
                For Each grp In query
                    For Each row In grp.ProductGroups
                        row.SetField("gnum", grpNumber)
                    Next
                    grpNumber += 1
                Next


            End Sub

所以我做错了什么?

以下是表格的样子,我想按cprice和ccategory列分组

Here is how the table looks like and i want to group by the columns cprice and ccategory

+---------------+---------------+-----------+------+
| Product Name  | cprice        | ccategory | gnum |
+---------------+---------------+-----------+------+
| Skirt Red     |            99 |         1 |      |
| Jeans Blue    |            49 |         2 |      |
| Jeans Black   |            49 |         2 |      |
| Skirt Blue    |            99 |         1 |      |
| T-shirt White |            20 |         2 |      |
| T-shirt Green |            20 |         2 |      |
| Jeans Grey    |            49 |         2 |      |
+---------------+---------------+-----------+------+


推荐答案

试试这个:

new with { Key.Price = row.Field(Of Double)("cprice") ,
           Key.Category = row.Field(Of Integer)("ccategory")

使用修饰符,这会创建一个匿名类型,它基于成员的平等实现相等性。

Uisng the Key modifier, this creates an anonymous type that implements equality based on equality of its members.

这篇关于VB.NET LINQ群组由多列组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆