Linq (to entities) serialization into XElement (with joins and group by)

ABethofFreshAir

New Member
What I am doing is query the database with Linq to entities to build an xml document. Before I was using store procedures with "for xml" clause and using an xmlreader, but now I am trying to stick with only one model to query the database, Linq to Entities.Now the code below is the best I could come with, but I really hate I could not find a better solution to build the XML at once without having to first query the db and then iterate to create the XElements.I could not create XElements directly in the select because the class doesn't have a parameterless creator, and as far as I know there's no ToXElement() in Linq.Anyone who did similar things before has any better idea on how to do that?\[code\] var bus = from bs in db.mela_buddies_store join mu in db.mela_users on bs.buddyId equals mu.userid join mup in db.mela_users_picnames on mu.userid equals mup.userid into pics from up in pics.DefaultIfEmpty() join bc in db.mela_buddies_store on mu.userid equals bc.buddyId into cntr from hm in cntr.DefaultIfEmpty() where bs.userId == lu.guid group bs by new { id = bs.ID, displayname = mu.displayname, gender = mu.gender, picname = up.picname } into final select new { id = final.Key.id, displayname = final.Key.displayname, gender = final.Key.gender, picname = final.Key.picname, hm = final.Count() }; XElement xe = new XElement("buddies", new XElement("userid", lu.userid), bus.ToList().Select(bs => new XElement("buddy", new XElement("id", bs.id), new XElement("displayname", bs.displayname), new XElement("gender", bs.gender), new XElement("picname", bs.picname), new XElement("hm", bs.hm) )));\[/code\]
 
Back
Top