Link to home
Start Free TrialLog in
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.
Avatar of kaufmed
kaufmed
Flag of United States of America image

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

Avatar of 5281
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
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 5281

ASKER

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