Group By Query

3 posts, 0 answers
  1. TWill
    TWill avatar
    1 posts
    Member since:
    Sep 2013

    Posted 11 Sep 2013 Link to this post

    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. Greg
    Greg avatar
    44 posts
    Member since:
    Jun 2012

    Posted 30 Oct 2013 Link to this post

    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 = ...
    }
  3. DevCraft banner
  4. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 04 Nov 2013 Link to this post

    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!
Back to Top