views:

816

answers:

3

I've got a query in C# that is working for me to query the metadata for the Entity Framework. I need to convert it to VB.NET, but I'm struggling to convert the AS keyword to "cast" meta to System.Data.Metadata.Edm.EntityType. I've tried TryCast, CType, Cast, etc.

Here's the query in C#:

var queryResult = from meta in oc.MetadataWorkspace.GetItems(System.Data.Metadata.Edm.DataSpace.CSpace)
                  .Where(m => m.BuiltInTypeKind == System.Data.Metadata.Edm.BuiltInTypeKind.EntityType)
                                  from p in (meta as System.Data.Metadata.Edm.EntityType).Properties
                                  .Where(p => p.DeclaringType.Name == entityClassType.Name
                                  && p.Name == propertyName)
                                  select p;

This is the closest I've come to getting it to compile in VB.NET (the As keyword in underlined and says ')' expected:

Dim query2 = _
             From meta In entityObjectContext.MetadataWorkspace.GetItems(System.Data.Metadata.Edm.DataSpace.CSpace) _
                  .Where(Function(m) m.BuiltInTypeKind = System.Data.Metadata.Edm.BuiltInTypeKind.EntityType) _
                  From p In (meta As System.Data.Metadata.Edm.EntityType).Properties _
                  .Where(Function(p) p.DeclaringType.Name = entity.GetType().Name _
                         And p.Name = propertyName) _
                  Select p

This is killing me. I'm so close...

+2  A: 

You can use CType to type cast:

...
From p In CType(meta, System.Data.Metadata.Edm.EntityType).Properties _
...

Update: Looking again at the query, I would suggest using OfType() instead:

From meta In entityObjectContext.MetadataWorkspace.GetItems(System.Data.Metadata.Edm.DataSpace.CSpace) _
             .OfType(Of System.Data.Metadata.Edm.EntityType)() _
From p In meta.Properties _
Where p.DeclaringType.Name = entity.GetType().Name _
    And p.Name = propertyName _
Select p

Update 2: Also, it looks like GetItems() has a generic version that I suspect will return only items of your desired type:

From meta In entityObjectContext.MetadataWorkspace.GetItems(Of System.Data.Metadata.Edm.EntityType)(System.Data.Metadata.Edm.DataSpace.CSpace) _
From p In meta.Properties _
Where p.DeclaringType.Name = entity.GetType().Name _
    And p.Name = propertyName _
Select p
dahlbyk
A: 

If I were writing this query in VB.NET, I would do it like this without the lambdas. I think it's easier to read, but they way you're doing it would probably be easier for a C# developer to read.

Dim query2 = _
    From meta In entityObjectContext.MetadataWorkspace.GetItems(System.Data.Metadata.Edm.DataSpace.CSpace) _
        Where m.BuiltInTypeKind = System.Data.Metadata.Edm.BuiltInTypeKind.EntityType _
            From p In CType(meta, System.Data.Metadata.Edm.EntityType).Properties _
                Where p.DeclaringType.Name = entity.GetType().Name _
                     And p.Name = propertyName) _
            Select p
Dennis Palmer
A: 

Thanks Dennis and dahlbyk, that worked!!! I like it without the lambas as well. More elegant and readable. You saved me plenty of hours...

D Pellegrin