My linq query works pretty good until i get to group by. I took a PluralSight class and it said that Linq can end in a Group By clause or a Select clause. However, in my VB.NET project, if i put Group By, it tells me "into is expected." These are very simple
queries. I'm able to get it to work, but... I have to use syntax that I don't understand. The PluralSight course was on C# but I've seen queries online that uses the same query syntax that I'm using.
Here is the object I created to test linq out.
PublicClassAuthorPrivate m_name AsStringPrivate m_age AsShortPrivate title AsStringPrivate mvp AsBooleanPrivate pubdate AsDateTimePrivate m_city AsStringPublicSubNew(name AsString, age AsShort, title AsString, mvp AsBoolean, pubdate AsDateTime, city AsString)
Me.m_name = name
Me.m_age = age
Me.title = title
Me.mvp = mvp
Me.pubdate = pubdate
Me.m_city = city
EndSubPublicProperty Name() AsStringGetReturn m_name
EndGetSet(value AsString)
m_name = value
EndSetEndPropertyPublicProperty Age() AsShortGetReturn m_age
EndGetSet(value AsShort)
m_age = value
EndSetEndPropertyPublicProperty BookTitle() AsStringGetReturn title
EndGetSet(value AsString)
title = value
EndSetEndPropertyPublicProperty IsMVP() AsBooleanGetReturn mvp
EndGetSet(value AsBoolean)
mvp = value
EndSetEndPropertyPublicProperty PublishedDate() AsDateTimeGetReturn pubdate
EndGetSet(value AsDateTime)
pubdate = value
EndSetEndPropertyPublicProperty City() AsStringGetReturn m_city
EndGetSet(value AsString)
m_city = value
EndSetEndPropertyEndClass
Afterwards I instantiate several objects and then try to create a Linq query.
Dim PeopleOfAtlanta = (From author In AuthorList
Where author.City = "Atlanta"Select author).ToList
Dim PeopleNames = (From author In AuthorList
Where author.City = "Atlanta"Select author.Name).ToList
'These two queries do not work:
Dim People = (From author In AuthorList
GroupBy author.City).ToList
Dim People2 = (From author In AuthorList Group author By author.City).ToList
'This gives me the error: "into is expected."
'If I add into and put a group name afterward, it will add parenthesis to the end. For example: Dim People = (From author In AuthorList
GroupBy author.City Into authorGroups()).ToList
'If i add a select statement was I was told during the PluralSight course (basically PluralSight said that if i add an INTO clause, 'i will need a select 'VS still adds parenthesis at the end of the into clause. Dim People = (From author In AuthorList
GroupBy author.City Into authorGroups()
Select authorGroups).ToList
'In addition to this, Visual Studio gives me red squeakly lines in Solution Explore. The error message I'm getting is "Definition of method 'authorGroups is not accessible in this context."
' The only thing I can get to work are the following:
Dim People = (From author In AuthorList
GroupBy author.City).ToList
Into AuthorsByCity = Group).ToList
' I don't understand why I need the into statement or set it equal to Group. I don't understand this syntax it seems counter-intuitive.
Group By Clause in VB is different from C#. Groups the elements of a query result. Can also be used to apply aggregate functions to each group. The grouping operation is based on one or more keys.
Syntax:
Group [ listField1 [, listField2 [...] ] By keyExp1 [, keyExp2 [...] ]
Into aggregateList
Member
23 Points
65 Posts
Why my LINQ query doesn't work was expected?
Mar 07, 2016 11:14 PM|JacobPressures|LINK
My linq query works pretty good until i get to group by. I took a PluralSight class and it said that Linq can end in a Group By clause or a Select clause. However, in my VB.NET project, if i put Group By, it tells me "into is expected." These are very simple queries. I'm able to get it to work, but... I have to use syntax that I don't understand. The PluralSight course was on C# but I've seen queries online that uses the same query syntax that I'm using.
Here is the object I created to test linq out.
Afterwards I instantiate several objects and then try to create a Linq query.
Star
12320 Points
2021 Posts
Re: Why my LINQ query doesn't work was expected?
Mar 08, 2016 03:07 AM|Candice Zhou|LINK
Hi jacobpressures,
Group By Clause in VB is different from C#. Groups the elements of a query result. Can also be used to apply aggregate functions to each group. The grouping operation is based on one or more keys.
Syntax:
Please see: https://msdn.microsoft.com/en-us/library/bb531412.aspx
Best Regards,
Candice Zhou
Member
23 Points
65 Posts
Re: Why my LINQ query doesn't work was expected?
Mar 08, 2016 08:34 PM|JacobPressures|LINK
Thanks! I got a book to try to help me understand the logic of this query. It seems unnecessarily verbose and confusing.