In mixing Linq-to-SQL and Linq-to-XML, I used to be able to do something like this:
XElement xml = new XElement("People");
xml.Add(from p in Context.People
select new XElement("Person",
new XElement("Id", p.Id),
new XElement("Name", p.Name)));
In converting some stuff to EF, I now get this exception: “Only parameterless constructors and initializers are supported in LINQ to Entities.”
This leads me to believe I now need to do something like this:
XElement xml = new XElement("People");
var peopleResults = Context.People.Select(p => { p.Id, p.Name }).ToList();
xml.Add(from p in peopleResults
select new XElement("Person",
new XElement("Id", p.Id),
new XElement("Name", p.Name)));
Is this my only alternative now, or is there another cleaner way to express this in code?
The approach is correct. To shorten it slightly you can use the ToList method on the object directly.