Reputation: 117
I am attempting a LINQ query in a VB.NET WPF solution that bridges two datatables and groups by a Zone.
I am receiving an "Invalid Cast Exception" on these rows:
.MfstCt = ZoneGroup.Count(Function(c) c.Field(Of String)("ConsignmentNumber")), _
.ArticleCt = ZoneGroup.Count(Function(c) c.Field(Of String)("ArticleNumber")) _
The error states:
Conversion from string "B4B0158234" to type 'Boolean' is not valid.
But I don't see any conversion to boolean in my query.
The field ConsignmentNumber
is in the mfst table, and the field ArticleNumber
is in the article table. I have added the entire query below. Can anyone help advise where I am going wrong with this?
Dim query = _
From Mfst In tblMFst.AsEnumerable() _
Join article In tblArticle.AsEnumerable() _
On Mfst.Field(Of Integer)("PCMSConsignment_ID") Equals _
article.Field(Of Integer)("PCMSConsignment_ID") _
Group Mfst By Zone = Mfst.Field(Of String)("PostZone") Into ZoneGroup = Group
Select New With _
{ _
Key Zone, _
.MfstCt = ZoneGroup.Count(Function(c) c.Field(Of String)("ConsignmentNumber")), _
.ArticleCt = ZoneGroup.Count(Function(c) c.Field(Of String)("ArticleNumber")) _
}
For Each x In query
Console.WriteLine(x.Zone, x.MfstCt, x.MfstCt)
Next
EDIT: I have changed code to reflect correct (Of String)
and updated the error message being received.
Upvotes: 0
Views: 840
Reputation: 8982
LINQ's .Count
extension method, when it takes a Func
predicate requires the function to return a boolean. See the documentation here. The function is there to act as a predicate to chose which elements to count (for example Function(x) x >= 20
).
I'm not sure exactly what you're trying to accomplish, but it seems like you should be just do something like ZoneGroup.Count()
or if you're trying to match a particular condition ZoneGroup.Count(Function(c) c.IsTheThingWeWant())
.
Upvotes: 1
Reputation: 8992
The Linq Count function expects a boolean comparison. You are passing:
.Count(Function(c) c.Field(Of String)("ConsignmentNumber"))
It is trying to return c.Field(Of String)("ConsignmentNumber")
as a boolean.
Upvotes: 0