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

LINQ Collection Error

1 Answer 160 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.
Muhammad
Top achievements
Rank 1
Muhammad asked on 13 May 2011, 11:36 AM
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]'.

1 Answer, 1 is accepted

Sort by
0
Thomas
Telerik team
answered on 13 May 2011, 04:11 PM
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.
Tags
LINQ (LINQ specific questions)
Asked by
Muhammad
Top achievements
Rank 1
Answers by
Thomas
Telerik team
Share this question
or