Since this is a linq-to-enities
query you can do this, no need to worry about null reference exceptions.
u.Media.FirstOrDefault().Url
Domanda
I am selecting an anonymous class in a LINQ-to-Entities query:
var users = (from u in db.Users.OrderByDescending(u => u.ID).Skip(skip).Take(200)
select new{
u.ID, u.FirstName, u.LastName, u.Gender, u.IsEnabled, u.Media
}).ToList();
The problem is with the media column. Media
is a navigation property, that may have zero or more objects. Media
is a class where it has a URL
property, which has its own table and navigation property on User
. I want to select an anonymous class such as:
select new{
u.ID, u.FirstName, u.LastName, u.Gender, u.IsEnabled, "u.Media's first items URL proprty, or null/empty string if u.Media is empty"
}).ToList();
Is this possible? If yes, how?
Soluzione
Since this is a linq-to-enities
query you can do this, no need to worry about null reference exceptions.
u.Media.FirstOrDefault().Url