Answered by:
Why my LINQ query doesn't work was expected?

Question
-
User686339638 posted
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.
Public Class Author Private m_name As String Private m_age As Short Private title As String Private mvp As Boolean Private pubdate As DateTime Private m_city As String Public Sub New(name As String, age As Short, title As String, mvp As Boolean, pubdate As DateTime, city As String) Me.m_name = name Me.m_age = age Me.title = title Me.mvp = mvp Me.pubdate = pubdate Me.m_city = city End Sub Public Property Name() As String Get Return m_name End Get Set(value As String) m_name = value End Set End Property Public Property Age() As Short Get Return m_age End Get Set(value As Short) m_age = value End Set End Property Public Property BookTitle() As String Get Return title End Get Set(value As String) title = value End Set End Property Public Property IsMVP() As Boolean Get Return mvp End Get Set(value As Boolean) mvp = value End Set End Property Public Property PublishedDate() As DateTime Get Return pubdate End Get Set(value As DateTime) pubdate = value End Set End Property Public Property City() As String Get Return m_city End Get Set(value As String) m_city = value End Set End Property End Class
Afterwards I instantiate several objects and then try to create a Linq query.
Dim AuthorList As New List(Of Author)() AuthorList.Add(New Author("Mahesh Chand", 35, "A Prorammer's Guide to ADO.NET", True, New DateTime(2003, 7, 10), "Mobile")) AuthorList.Add(New Author("Neel Beniwal", 18, "Graphics Development with C#", False, New DateTime(2010, 2, 22), "Mobile")) AuthorList.Add(New Author("Praveen Kumar", 28, "Mastering WCF", True, New DateTime(2012, 1, 1), "Mobile")) AuthorList.Add(New Author("Mahesh Chand", 35, "Graphics Programming with GDI+", True, New DateTime(2008, 1, 20), "Mobile")) AuthorList.Add(New Author("Raj Kumar", 30, "Building Creative Systems", False, New DateTime(2011, 6, 3), "Mobile")) AuthorList.Add(New Author("Mahesh Patel", 35, "A Prorammer's Guide to ADO.NET", True, New DateTime(2003, 7, 10), "Atlanta")) AuthorList.Add(New Author("Neel Patel", 18, "Graphics Development with C#", False, New DateTime(2010, 2, 22), "Atlanta")) AuthorList.Add(New Author("Praveen Patel", 28, "Mastering WCF", True, New DateTime(2012, 1, 1), "Atlanta")) AuthorList.Add(New Author("Mahesh Patel", 35, "Graphics Programming with GDI+", True, New DateTime(2008, 1, 20), "Pensacola")) AuthorList.Add(New Author("Raj Patel", 30, "Building Creative Systems", False, New DateTime(2011, 6, 3), "Pensacola"))
'These two queries work with no problems:
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 Group By 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 Group By 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 Group By 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 Group By 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.
'Thanks very much!Monday, March 7, 2016 11:14 PM
Answers
-
User614698185 posted
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:
Group [ listField1 [, listField2 [...] ] By keyExp1 [, keyExp2 [...] ] Into aggregateList
Please see: https://msdn.microsoft.com/en-us/library/bb531412.aspx
Best Regards,
Candice Zhou
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 8, 2016 3:07 AM
All replies
-
User614698185 posted
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:
Group [ listField1 [, listField2 [...] ] By keyExp1 [, keyExp2 [...] ] Into aggregateList
Please see: https://msdn.microsoft.com/en-us/library/bb531412.aspx
Best Regards,
Candice Zhou
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, March 8, 2016 3:07 AM -
User686339638 posted
Thanks! I got a book to try to help me understand the logic of this query. It seems unnecessarily verbose and confusing.
Tuesday, March 8, 2016 8:34 PM