5

I am trying to load a partial entity with Linq to Entities:

Dim contacts = From c In My.Context.Contacts _
     Select New Contact With { _
         .ContactId = c.ContactId, _
         .Name = c.Name
     }

I tried it and I get the following NotSupportedException: "The entity or complex type 'CompleteKitchenModel.Contact' cannot be constructed in a LINQ to Entities query."

Thanks

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632

1 Answers1

2

You'll have to use anonymous type:

Dim contacts = From c In My.Context.Contacts _
 Select New With { _
     .ContactId = c.ContactId, _
     .Name = c.Name
 }

and then copy data to Contact list:

For Each contact In contacts    
     Dim c As New Contact With { .ContactId = c.ContactId, .Name = c.Name}
     //Add to list
Next

Your syntax, as error says, is not supported.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
LukLed
  • 31,452
  • 17
  • 82
  • 107
  • 1
    +1. Alternately, project onto a POCO, which is supported in L2E. – Craig Stuntz Jan 06 '10 at 14:14
  • @Craig Fisher: And where did I wrote something about syntax error? – LukLed Dec 16 '10 at 09:50
  • @Craig Stuntz, @Craig Fisher, @LukLed, please refer to [my related question](http://stackoverflow.com/q/5985357 "DTOs in WCF RIA Services Master-Detail") on how to load partial queries using L2E across the wire with RIA Services. – Shimmy Weitzhandler May 16 '11 at 01:32