Avatar of 5281
5281
 asked on

How to do this Linq query with group by?

I put two database tables into two generic class lists.  I want to translate the following sql query into  LINQ Query, but I keep getting errors.

SELECT a.ArticleNumber, a.ArticleType, a.AuthorID, b.AuthorID, b.AuthorName
FROM Article AS a INNER JOIN Author as b ON a.AuthorID=b.AuthorID
WHERE a.ArticleNumber=myNumber
GROUP BY a.ArticleNumber, b.AuthorID, b.AuthorName, a.ArticleType

Open in new window



I tried the following, but there is error for ArticleGroup(), definition of method 'ArticleGroup'  is not accessible in this context.

Dim myNumber as string
Dim ArticleTableList as New List(of clsPaper)     'Class type List holds table Ariticle
Dim AuthorTableList as New List(of clsPeople)  'Class type List holds table Author

Dim CountAritle As clsPaper = (From c In ArticleTableList Join d In AuthorTableList On c.AuthorID Equals d.AuthorID  _
Where c.ArticleNumber = myNumber Group c By c.ArticleNumber, d.AuthorID, d.AuthorName, c.ArticleType _
 Into ArticleGroup() Select c).SingleOrDefault()

Open in new window



I am using ASP.NET in VB.
Visual Basic.NETASP.NETC#LINQ Query

Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon
kaufmed

It appears you have a method named ArticleGroup within this code somewhere. That name is in conflict with what you are trying to call your group. Try renaming it in your query.

e.g.

Dim CountAritle As clsPaper = (From c In ArticleTableList Join d In AuthorTableList On c.AuthorID Equals d.AuthorID  _
Where c.ArticleNumber = myNumber Group c By c.ArticleNumber, d.AuthorID, d.AuthorName, c.ArticleType _
 Into theGroup Select c).SingleOrDefault()

Open in new window

5281

ASKER
I renamed it, it still the same error, .NET compiler automatically add a parentheses behind group name theGroup() in this case.
I think we need rewrite this linq query.  Somewhere is not right.
ASKER CERTIFIED SOLUTION
Fernando Soto

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
5281

ASKER
Wow.  Excellent.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Fernando Soto

Not a problem 5281, glad I was able to help.