I’m getting this error:
Only parameterless constructors and initializers are supported in LINQ to Entities.
When trying to run this code (found this code here and made test database to play around with):
XElement xml = new XElement("contacts",
from c in db.Contacts
orderby c.ContactId
select new XElement("contact",
new XAttribute("contactId", c.ContactId),
new XElement("firstName", c.FirstName),
new XElement("lastName", c.LastName))
);
where db is the auto created entities object. Any ideas on how to get this to work?
I believe it’s objecting to the fact that you’re using an XElement constructor that takes parameters in your “select” clause. Since XElement doesn’t have a parameterless constructor, you might need to change your code to select an anonymous type, and initialize the XElement collection after the fact.
That’s untested, but hopefully gives you the idea. I’m doing the EF query first, and then calling ToList() on it so that I can select the XElement collection using Linq to Objects rather than EF.