Maybe a simple question but I have a List<object>
that I use to return just a handful of properties from my full User class, like first name, last name, phone number.
But my question is why does this work....
public List<object> GetObject(IQueryable query)
{
// ...
List<object> smallObj = query.Select(u => GetSmallUser(u)).ToList();
// ...
}
private object GetSmallUser(User user)
{
return new{
FirstName = user.FirstName,
LastName = user.LastName,
Phone = user.PhoneNumber
}
}
And this doesn't work...
public List<object> GetObject(IQueryable query)
{
// ...
List<object> smallObj = query.Select(u => new
{
FirstName = u.FirstName,
LastName = u.LastName,
Phone = u.PhoneNumber
}).ToList();
// ...
}
The later is saying that it convert List of anonymous type to a list of object. Shouldn't those be returning the same thing?