I have the following entities:
public class Movie
{
public int MovieID { get; set; }
public int? DirectorID { get; set; }
public string Title { get; set; }
public Person Director { get; set; }
public ICollection<Person> Actors { get; set; }
}
//Director Config:
//HasOptional(m => m.Director).WithMany().HasForeignKey(m => m.DirectorID);
public class Person
{
public int PersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
//Other fields....
}
The Person entity has other fields than the 3 I've shown but those are the 3 I'm interested in. For the purposes of creating a web page to edit a movie entity I wish to return data like this:
var q = _ctx.Movies.Where(m => m.MovieID == movieID);
q = q.Include(m => m.Director);
return q.Select(m => new
{
MovieID = m.MovieID,
Name = m.Title,
Director = m.Director == null ? null : new { id= m.Director.PersonID, text=m.Director.LastName}
}).Single();
The goal being to return an object with a Director property that is null if no Director has been set or that is an object with 2 properties just id and text (name) if a Director has been set. It would be overkill to return to the client the entire person entity for this scenario. Unfortunately the above query won't work.
Are there any options for doing something like this? Alternatives?
Thanks very much for any help.