Search code examples
silverlightentity-frameworkwcf-ria-services

Return "custom made" entities in a LinqToEntitiesDomainService without mapping them to the DB?


(I searched a lot and didn't find an answer yet, maybe it is not possible at all:/)

Is it possible to create entities in the EDM (from which I generated my DomainService) that are not mapped at all to any DB, just used instead between the DomainService and the Client? It might sound wierd for those who are entity framework pros:) My goal is to make it possible to return for example only a few properties of an entity instead of the whole entity. For example I need only the name and birthdate of a user, and I don't care about the other 10-20 properties. In this case a less complex entity would be perfect, is it possible to make these "lightweight" versions of the entities and use them in the same DomainService? Or any other suggestions for this kind of simple data querying scenario, using EF on top of the DB? (Or maybe just don't care about the bandwith in 2011 and retrieve full entities always? :)

Thanks, Bálint


Solution

  • You can load anonymous types by selecting sevaral properties of your entity like this,

     var studentEnrolments = from s in db.Students
                                      select new { s.FirstMidName,s.Enrollments};
            var studentEnrolmentsList= studentEnrolments.ToList();
            foreach (var studentEnrolment in studentEnrolmentsList)
            {
               //do something here 
            }
    

    http://msdn.microsoft.com/en-us/library/bb738512.aspx

    Edit..

     var studentEnrolments = from s in db.Students
                           select new Student{FirstMidName= s.FirstMidName, Enrollments=s.Enrollments};