LINQ Collection Error

2 posts, 0 answers
  1. Muhammad
    Muhammad avatar
    87 posts
    Member since:
    Oct 2012

    Posted 13 May 2011 Link to this post

    I am using Linq Query and want to return the collection and iterate all database fields but on the bold line below I got the following error.

     Dim IGroups = From Groups In dbContext.FF_Groups
                          Where Groups.TariffID = nTariffID
                          Select Groups.ID, Groups.GroupName, Groups.GroupOrder, Groups.IsDeleted, Groups.TariffID, Groups.CreatedOn, Groups.CreatedBy

    Error Occurs in that line :

    Dim IGroupList As IList(Of FFDataLayer.FFDataLayer.FF_Group) = IGroups.ToList

    Error:

    Unable to cast object of type 'System.Collections.Generic.List`1[VB$AnonymousType_1`7[System.Int64,System.String,System.Nullable`1[System.Int64],System.Boolean,System.Int64,System.DateTime,System.Int64]]' to type 'System.Collections.Generic.IList`1[FFDataLayer.FFDataLayer.FF_Group]'.
  2. Thomas
    Admin
    Thomas avatar
    590 posts

    Posted 13 May 2011 Link to this post

    Hello Muhammad,

    this looks like a VB cast issue: the return value of the LINQ statement is a List of an anonymous type. This list cannot be assigned to a field of the type that you wanted. I think you will need to tweak your VB LINQ query so that not an anonymous type is populated, but a known type. 

    Dim IGroups = From Groups In dbContext.FF_Groups
                          Where Groups.TariffID = nTariffID
                          Select New FFDataLayer.FFDataLayer.FF_Group With { .ID = Groups.ID, .GroupName = Groups.GroupName, .GroupOrder = Groups.GroupOrder, .IsDeleted = Groups.IsDeleted, .TariffID= Groups.TariffID, .CreatedOn = Groups.CreatedOn, .CreatedBy = Groups.CreatedBy }

    Alternativly if you do not want a projection at all but leave all up to OpenAccess:

    Dim IGroups = From Groups In dbContext.FF_Groups
                          Where Groups.TariffID = nTariffID
                          Select Groups

    That will then fill in the default fetch group which is most likely the thing you want.

    Greetings,
    Thomas
    the Telerik team
    Q1’11 SP1 of Telerik OpenAccess is available for download; also available is the Q2'11 Roadmap for Telerik OpenAccess ORM.
  3. DevCraft banner
Back to Top