tags:

views:

18

answers:

1

Hi all,

I've got a problem with Linq in VB.NET. I've created a .dbml file from my db, and everything looks good - relation "arrows" between tables are visible, etc.

But when I want to use simple query (taken from 101 Linq Samples by MS):

Dim q = From h In dc.Hours, w In h.WorkType

I receive an error:

Expression of type 'INTegrity.WorkType' is not queryable. Make sure you are not missing an assembly reference and/or namespace import for the LINQ provider.

h.Worktype is visible in Intellisense...

I can manually join in the Linq query, but shouldn't the relations be automatic?

The full query that works, but using "manual joins" looks like this:

Dim q1 = From g In dc1.Hours _ 
Join pr In dc1.WorkType On g.WorkTypeId Equals pr.WorkTypeId _ 
Where g.WorkerId = workerid _ 
Select New With {g.EntryId, g.Date, pr.WorkTypeDesc, g.Minutes} 

Worktype table has 1 to many relationship with Hours table (as you can see on the WorkTypeId column)

+1  A: 

I don't speak LINQ in VB, so I'll try to adapt to C#

From h In dc.Hours, w In h.WorkType 

from h in dc.Hours
from w in h.WorkType
.....

This implies that WorkType is a collection, which it's name does not suggest. I think you want something closer to

from h in dc.Hours
let w = h.WorkType 
....

Of course, none of these will do anything without the parts in the "....". If you show us what you want to do there, we could probably fix it for you.

UPDATE#1:

Trying to piece together what you are doing, let's guess that Hours & WorkType are tables with a one-to-many relationship (WorkType on the "one" side), hence every Hours record matchs one WorkType record. In that case, Linq-to-SQL will automatically generate a scalar WorkType property in Hours. You don't need the "from ... in " to access it. It's just a property, use it directly where you need it.

UPDATE#2: (From comments) I think this should work:

Dim q1 = 
From g In dc1.Hours 
Where g.WorkerId = workerid 
Select New With {g.EntryId, g.Date, g.WorkType.WorkTypeDesc, g.Minutes} 
James Curran
It's even simpler than I thought. Thanks James!
ufoq