这个linq用vb.net的语法怎么写?

clear_zero 2012-05-29 04:18:07
我有一个class(clsStudent),里面有StudentID,Name,Age,ClassID,Gender

然后我有个 lstStudent是clsStudent的list

我现在想知道在lstStudent里面有多少学生有相同Age,ClassID,Gender;并把这些学生的Name和StudentID打印出来

我在网上看的似乎语法不对,麻烦指点下



from s as clsStudent in lstStudent group s by new {s.Age, s.Gender, s.ClassID} into mygrp ...‘这样就通不过,后面不会写了


如果我这么写不对,那如何才能写才能达到我的要求呢

谢谢
...全文
617 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
clear_zero 2012-05-30
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 的回复:]

VB.NET code

'发觉这两种方式都可以,发楼主参考:

Dim query1=From x in empList _
Group x By x.Age, x.Sex Into g=Group _
Select New With {g,g.Count()}

Dim query2……
[/Quote]
我这里第二种不成...我用的是VS2008

谢谢你
q107770540 2012-05-30
  • 打赏
  • 举报
回复

'发觉这两种方式都可以,发楼主参考:

Dim query1=From x in empList _
Group x By x.Age, x.Sex Into g=Group _
Select New With {g,g.Count()}

Dim query2=From el In empList _
Group el By Key = new with {key el.Age, key el.Sex} _
Into Group _
Select New With {.key = Key, _
.count = Group.Count()}
clear_zero 2012-05-29
  • 打赏
  • 举报
回复
我来补充下即便是一个条件



Dim query= From s in lstStudent _
Group by k= New With {s.Age} into g = Group _
Select New with _
{ .StudentID=String.Join(",",g.Select(Function(x) x.StudentID).ToArray()), _
.Name=String.Join(",",g.Select(Function(x) x.Name).ToArray()), _
.Count=g.Count() _
}


这样还是不会出现group by 所要的结果,还是每一个都算了
clear_zero 2012-05-29
  • 打赏
  • 举报
回复
论坛好了阿,我刚才发言论坛维护。我已经弄出来了


Dim query= From s in lstStudent _
Group s by s.Age,s.Gender, s.ClassID into g = Group _
Select New with _
{ .StudentID=String.Join(",",g.Select(Function(x) x.StudentID).ToArray()), _
.Name=String.Join(",",g.Select(Function(x) x.Name).ToArray()), _
.Count=g.Count() _
}



这样就是我要的结果,看来关键是new with这里;具体怎么为什么不同只能高人来讲解了。关键是new with这个编译过了

祝Tim答辩顺利
threenewbee 2012-05-29
  • 打赏
  • 举报
回复
有意思,我手上没有环境,晚上来研究下。
q107770540 2012-05-29
  • 打赏
  • 举报
回复
你也可随时关注这个帖子,我已经在MSDN上提出了疑问:

http://social.microsoft.com/Forums/zh-CN/adonetzhchs/thread/7f06ff9e-a704-411e-9220-7a86963a290e
q107770540 2012-05-29
  • 打赏
  • 举报
回复
刚做了个DEMO,同样的数据,用VB.NET 和C# 进行分组,得到的数据竟然不一样

Sub Main
Dim empList As New List(Of Employee)()
empList.Add(New Employee() With _
{.ID = 1, .FName = "John", .Age = 23, .Sex = "M"c})
empList.Add(New Employee() With _
{.ID = 2, .FName = "Mary", .Age = 25, .Sex = "F"c})
empList.Add(New Employee() With _
{.ID = 3, .FName = "Amber", .Age = 23, .Sex = "M"c})
empList.Add(New Employee() With _
{.ID = 4, .FName = "Kathy", .Age = 25, .Sex = "F"c})
empList.Add(New Employee() With _
{.ID = 5, .FName = "Lena", .Age = 27, .Sex = "F"c})
empList.Add(New Employee() With _
{.ID = 6, .FName = "Bill", .Age = 28, .Sex = "M"c})
empList.Add(New Employee() With _
{.ID = 7, .FName = "Celina", .Age = 27, .Sex = "F"c})
empList.Add(New Employee() With _
{.ID = 8, .FName = "John", .Age = 28, .Sex = "M"c})

Dim query = empList.GroupBy(Function(x) New With { .Age=x.Age, .Sex= x.Sex}) _
.Select(Function(g) New With {g.Key, g.Count()})

For Each employee In query
Console.WriteLine(employee.Count)
Next employee

End Sub

Public Class Employee
Private privateID As Integer
Public Property ID() As Integer

Get
Return privateID
End Get

Set(ByVal value As Integer)
privateID = value
End Set

End Property

Private privateFName As String
Public Property FName() As String
Get
Return privateFName
End Get

Set(ByVal value As String)
privateFName = value
End Set
End Property

Private privateAge As Integer
Public Property Age() As Integer
Get
Return privateAge
End Get

Set(ByVal value As Integer)
privateAge = value
End Set
End Property

Private privateSex As Char
Public Property Sex() As Char
Get
Return privateSex
End Get

Set(ByVal value As Char)
privateSex = value
End Set
End Property
End Class






void Main()
{
var empList =new List<Employee>
{
new Employee {ID = 1, FName = "John", Age = 23, Sex = 'M'},
new Employee {ID = 2, FName = "Mary", Age = 25, Sex = 'F'},
new Employee {ID = 3, FName = "Amber", Age = 23, Sex = 'M'},
new Employee {ID = 4, FName = "Kathy", Age = 25, Sex = 'F'},
new Employee {ID = 5, FName = "Lena", Age = 27, Sex = 'F'},
new Employee {ID = 6, FName = "Bill", Age = 28, Sex = 'M'},
new Employee {ID = 7, FName = "Celina", Age = 27, Sex = 'F'},
new Employee {ID = 8, FName = "John", Age = 28, Sex = 'M'}
};

var query = empList.GroupBy(x => new { x.Age, x.Sex})
.Select(g=>new {g.Key, Count=g.Count()});

foreach (var employee in query )
Console.WriteLine(employee.Count);

}
public class Employee
{
public int ID {get;set;}
public string FName {get;set;}
public int Age {get;set;}
public char Sex {get;set;}
}


明天论文答辩回来再研究一下,暂时未找到错在哪里 。。。
q107770540 2012-05-29
  • 打赏
  • 举报
回复
LINQ 是支持多列分组的 这个毋庸置疑
clear_zero 2012-05-29
  • 打赏
  • 举报
回复
上面有一个错误

dim lstStudent as new list(of clsStudent)

for i as integer =0 to 3

dim tempS as new clsStudent
with tempS
.Age=15
.Gender="M"
.ClassID=1
.StudentID=i
.Name="Test_" & i
end with
lstStudent.add (tempS)
next

clear_zero 2012-05-29
  • 打赏
  • 举报
回复
好的


dim lstStudent as new list(of clsStudent)

for i as integer =0 to 3

dim tempS as new clsStudent
with tempS
.Age=15
.Gender=M
.ClassID=1
.StudentID=i
.Name="Test_" & i
end with
lstStudent.add (tempS)
next



就是这么一个测试数据
threenewbee 2012-05-29
  • 打赏
  • 举报
回复
贴一些测试数据上来。
clear_zero 2012-05-29
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]

Dim query= From s in lstStudent _
Group s by New With {s.Age, s.Gender, s.ClassID} into g = Group _
Select New with _
{ _
.Age=g.Key.Age , _
……
[/Quote]

谢谢你的帮助我这样语法虽然写过了

Dim query= From s in lstStudent _
Group by k= New With {s.Age, s.Gender, s.ClassID} into g = Group _
Select New with _
{ _
.Age=k.Age , _
.Gender=k.Gender, _
.ClassID=k.ClassID, _
.StudentID=String.Join(",",g.Select(Function(x) x.StudentID).ToArray()), _
.Name=String.Join(",",g.Select(Function(x) x.Name).ToArray()), _
.Count=g.Count() _
}
结果没有起到group by 的作用;原来多少还是多少。每一个count都是1
但是我准备的数据里面有重复的,可惜没有去掉

但是如果我只选择其中一个属性作为group by的标准就会成功
Dim query= From s in lstStudent _
Group s by s.Age into g = Group _
Select New with _
{ .StudentID=String.Join(",",g.Select(Function(x) x.StudentID).ToArray()), _
.Name=String.Join(",",g.Select(Function(x) x.Name).ToArray()), _
.Count=g.Count() _
}

难道说linq不支持多列作为goup by 的条件呢?我尝试了两个Age,Gender都不可以。不知道是不是可以冒昧的请你作个实验。看看是不是本来就不支持还是哪里需要改进?

谢谢
q107770540 2012-05-29
  • 打赏
  • 举报
回复
Dim query= From s in lstStudent _
Group s by New With {s.Age, s.Gender, s.ClassID} into g = Group _
Select New with _
{ _
.Age=g.Key.Age , _
.Gender=g.Key.Gender, _
.ClassID=g.Key.ClassID, _
.StudentID=String.Join(",",g.Select(Function(x) x.StudentID).ToArray()), _
.Name=String.Join(",",g.Select(Function(x) x.Name).ToArray()), _
.Count=g.Count() _
}

q107770540 2012-05-29
  • 打赏
  • 举报
回复

Dim query= From s in lstStudent _
Group s by New With {s.Age, s.Gender, s.ClassID} into g = Group _
Select New with _
{
.Age=g.Key.Age , _
.Gender=g.Key.Gender, _
.ClassID=g.Key.ClassID, _
.StudentID=String.Join(",",g.Select(Function(x) x.StudentID).ToArray()), _
.Name=String.Join(",",g.Select(Function(x) x.Name).ToArray()), _
.Count=g.Count() _
}
clear_zero 2012-05-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

VB.NET code
Dim query= From s in lstStudent _
Group s by New With {s.Age, s.Gender, s.ClassID} into g = Group _
Select New with
{
.Age=g.Key.Age ,
……
[/Quote]
谢谢阿

这个还是不行

问题出在
Dim query= From s in lstStudent _
Group s by New With {s.Age, s.Gender, s.ClassID} into g = Group _
Select New with
{
.Age=g.Key.Age , '--g.key这个不认
.Gender=g.Key.Gender,
.ClassID=g.Key.ClassID,
.StudentID=string.Join(",",g.Select(x=>x.StudentID).ToArray()),'假定 StudentID为string 类型
.Name=string.Join(",",g.Select(x=>x.Name).ToArray()),'假定 Name为string 类型
.Count=g.Count()
}
还有就是x也不认

另外我按照你给的连接的如下例子写了一个
Dim categories = From p In db.Products _
Group By Key = New With {p.CategoryID, p.SupplierID} Into Group _
Select Key, Group
也不行,发现虽然通过了,但是并没有起到group by 的作用。

不知道还有什么办法,谢谢
q107770540 2012-05-29
  • 打赏
  • 举报
回复
Dim query= From s in lstStudent _
Group s by New With {s.Age, s.Gender, s.ClassID} into g = Group _
Select New with
{
.Age=g.Key.Age ,
.Gender=g.Key.Gender,
.ClassID=g.Key.ClassID,
.StudentID=string.Join(",",g.Select(x=>x.StudentID).ToArray()),'假定 StudentID为string 类型
.Name=string.Join(",",g.Select(x=>x.Name).ToArray()),'假定 Name为string 类型
.Count=g.Count()
}
clear_zero 2012-05-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

var query=from s in lstStudent
group s by new {s.Age, s.Gender, s.ClassID} into g
select new
{
Age=g.Key.Age,
Gender=g.Key.Gender,
……
[/Quote]
谢谢,这个怎么写成vb.net的语法阿?
上面的在vb.net里面通不过

dim query=from s in lstStudent
group s by new with {s.Age, s.Gender, s.ClassID} into g
select new with
{
Age=g.Key.Age,
Gender=g.Key.Gender,
ClassID=g.Key.ClassID,
StudentID=string.Join(",",g.Select(x=>x.StudentID).ToArray()),//假定 StudentID为string 类型
Name=string.Join(",",g.Select(x=>x.Name).ToArray()),//假定 Name为string 类型
Count=g.Count()
}

改成这样后,就是Anonymous type member name must be preceded by a period的错误
麻烦了
q107770540 2012-05-29
  • 打赏
  • 举报
回复
var query=from s in lstStudent
group s by new {s.Age, s.Gender, s.ClassID} into g
select new
{
Age=g.Key.Age,
Gender=g.Key.Gender,
ClassID=g.Key.ClassID,
StudentID=string.Join(",",g.Select(x=>x.StudentID).ToArray()),//假定 StudentID为string 类型
Name=string.Join(",",g.Select(x=>x.Name).ToArray()),//假定 Name为string 类型
Count=g.Count()
};
.NET 3.5与VB 2008高级编程(第3版)高清扫描完整版。 原书名:Pro VB 2008 and the .NET 3.5 Platform 由于文件太大,分2卷压缩上传,请全部下载后解压,压缩包内含配书源代码,这是第1卷。访问:http://download.csdn.net/user/zgc988可以方便查找第2卷的下载地址。 ============================================================ 内容简介 本书的第1版出版于2001年在乔治亚州亚特兰大市举办的Tech·Ed会议之后不久。本书的最新版是以前版本的全新升级,它阐述了.NET 3.0和.NET 3.5中包含的所有新特性。 .NET 3.0 没有改变VB的语法,它只是“增强版”,实际上提供了3种新的API:Windows Presentation Foundation(WPF) 、Windows Communication Foundation(WCF) 和Windows Workflow Foundation (WF)。本书还讨论了W's 。 有别于.NET 3.0,.NET 3.5提供了许多新的VB 语言特性和新的.NET API。本书将使用与前面版本同样通俗易懂的方法,详细介绍所有这些新内容。本书将详细讨论语言级集成查询(Language Integrated Query,LINQ) 、VB 2008语言变更(对象初始化语法、扩展方法、匿名类型等)以及Visual Studio 2008的许多附加功能。 本书的任务就是为VB 2008 语言和面向对象编程技术以及.NET平台的核心问题奠定坚实基础。掌握了本书的内容之后,您就可以自如地将这些知识应用于特定的编程任务,从而根据自己的想法探索.NET世界。
.NET 3.5与VB 2008高级编程(第3版)高清扫描完整版。 原书名:Pro VB 2008 and the .NET 3.5 Platform 由于文件太大,分2卷压缩上传,请全部下载后解压,压缩包内含配书源代码,这是第2卷。访问:http://download.csdn.net/user/zgc988可以方便查找第1卷的下载地址。 ============================================================ 内容简介 本书的第1版出版于2001年在乔治亚州亚特兰大市举办的Tech·Ed会议之后不久。本书的最新版是以前版本的全新升级,它阐述了.NET 3.0和.NET 3.5中包含的所有新特性。 .NET 3.0 没有改变VB的语法,它只是“增强版”,实际上提供了3种新的API:Windows Presentation Foundation(WPF) 、Windows Communication Foundation(WCF) 和Windows Workflow Foundation (WF)。本书还讨论了W's 。 有别于.NET 3.0,.NET 3.5提供了许多新的VB 语言特性和新的.NET API。本书将使用与前面版本同样通俗易懂的方法,详细介绍所有这些新内容。本书将详细讨论语言级集成查询(Language Integrated Query,LINQ) 、VB 2008语言变更(对象初始化语法、扩展方法、匿名类型等)以及Visual Studio 2008的许多附加功能。 本书的任务就是为VB 2008 语言和面向对象编程技术以及.NET平台的核心问题奠定坚实基础。掌握了本书的内容之后,您就可以自如地将这些知识应用于特定的编程任务,从而根据自己的想法探索.NET世界。

16,557

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧