This is a migrated thread and some comments may be shown as answers.

Group By Query

2 Answers 69 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
TWill
Top achievements
Rank 1
TWill asked on 11 Sep 2013, 02:55 PM
I have the following group by LINQ query in C# that I'm trying to rewrite in VB, but I get an error on my vb code. Can anyone see what is wrong with it.
C#
var dupValues = from mrows in dtLoadedData.Select()
group mrows by new
{
a = mrows.Field<object>(compositeColls[0].ColumnName),
b = mrows.Field<object>(compositeColls[1].ColumnName),
c = mrows.Field<object>(compositeColls[2].ColumnName),
d = mrows.Field<object>(compositeColls[3].ColumnName)
}
into grp where grp.Count() >= 2 select new { grp.Key.a, grp.Key.b, grp.Key.c, grp.Key.d, e = grp.Count() };
VB
Dim dupValues = From grp In From mrows In dtLoadedData.[Select]() Group mrows By New With { _
a = mrows.Field(Of Object)(compositeColls(0).ColumnName), _
b = mrows.Field(Of Object)(compositeColls(1).ColumnName), _
c = mrows.Field(Of Object)(compositeColls(2).ColumnName), _
d = mrows.Field(Of Object)(compositeColls(3).ColumnName) _
} Into grp() Where grp.Count() >= 2 Select New With { _
grp.Key.a, grp.Key.b, grp.Key.c, grp.Key.d, Key .e = grp.Count() }

Error: Anonymous type member name must be preceded by a period

2 Answers, 1 is accepted

Sort by
0
Greg
Top achievements
Rank 1
answered on 30 Oct 2013, 10:18 AM
As the error message states anonymous type initialization syntax in VB requires a period before the property name. E.g.
New With _
{
 .a = mrows.Field...
 .b = ...
}
0
Doroteya
Telerik team
answered on 04 Nov 2013, 01:04 PM
Hello TWill and Greg,

Indeed, as Greg points, declaring an anonymous type in VB requires a period before the names of the properties. This article can provide you with details about the subject.

@TWill,
Additionally, I would suggest to you to try rewriting the query that causes the error like this:
Dim dupValues = From mrows In dtLoadedData.Select()
           Group mrows By GroupKey = New With
           {
               .a = mrows.Field(Of Object)(compositeColls(0).ColumnName),
               .b = mrows.Field(Of Object)(compositeColls(1).ColumnName),
               .c = mrows.Field(Of Object)(compositeColls(2).ColumnName),
               .d = mrows.Field(Of Object)(compositeColls(3).ColumnName)
           } Into grp = Group
           Where grp.Count() >= 2
           Select New With
           {
               GroupKey.a,
               GroupKey.b,
               GroupKey.c,
               GroupKey.d,
               .e = grp.Count()
           }

I hope you find this feasible. If you have additional questions, or experience difficulties with the suggested solution, do not hesitate to get back to us.



Regards,
Doroteya
Telerik
OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
Tags
LINQ (LINQ specific questions)
Asked by
TWill
Top achievements
Rank 1
Answers by
Greg
Top achievements
Rank 1
Doroteya
Telerik team
Share this question
or