Title might be confusing, so let me explain. Let's pretend I have a Dbo that looks like this:
public class EmployeeDbo
{
public int EmployeeId { get; set; }
public string FirstName{ get; set; }
public string LastName{ get; set; }
public string EmployeeData { get; set; }
public string EvenMoreEmployeeData{ get; set; }
}
And then somewhere in my data access layer I have this code:
// employeeId is some param that gets passed into this method
var query = _dbContext.Employees.Where(e => e.EmployeeId = employeeId);
// Now I want to further modify the query. I just want to load SOME values from the DB, instead of all of them
// The problem I have is that now this is trying to assign query, which is IQueryable<EmployeeDbo> to IQueryable<{string FirstName, string LastName}>
query = query.Select(e => new
{
e.FirstName,
e.LastName
});
// More code here that is expecting IQueryable<EmployeeDbo>
So, to summarize my question, how can I just populate SOME properties in a dbo with data from the DB? I suppose I could create a new Dbo with only the data I want as properties, but can I avoid doing that?
query.Select(e => new EmployeeDbo { EmployeeId = 0, FirstName = e.FirstName, LastName = e.LastName })The rest of the properties would be null.new EmployeeDbohere, you're (probably) no longer working with a tracked entity. That is probably fine for Read\Query operations in a service type, but could pose issues in DAL. The scope of this concern relates to the reasoning of using Data Transitioning\Transferable Objects (DTOs).cannot be constructed in a LINQ to Entities query.