I have defined the following entity classes for my LINQ query:
public class Application
{
public Application() { }
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public System.DateTime DateTimeCreated { get; set; }
public System.DateTime? DateTimeModified { get; set; }
public Employee CreatedBy { get; set; }
public Employee ModifiedBy { get; set; }
}
public class Employee
{
public Employee() { }
public string EmployeeID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
I have created the following query to create an Application
object and trying to create an Employee
entity for the CreatedBy
and 'ModifiedBy' properties. Sometimes, the ModifiedBy
column can contain null and I want to set the ModifiedBy
property to null.
var query = from a in context.Applications
join u1 in context.Employees on a.CreatedBy equals u1.Employee_ID.Trim()
join u2 in context.Employees on a.ModifiedBy equals u2.Employee_ID.Trim()
where a.ApplicationId == applicationId
select new Entity.Application
{
Id = a.ApplicationId,
Name = a.ApplicationName,
Description = a.ApplicationDesc,
DateTimeCreated = a.DateTimeCreated,
CreatedBy = new Entity.Employee{ EmployeeID = a.CreatedBy, FirstName = u1.First_Name, LastName = u1.Last_Name },
DateTimeModified = a.DateTimeModified ?? null,
ModifiedBy = (a.ModifiedBy != null) ? new Entity.Employee { EmployeeID = a.ModifiedBy, FirstName = u2.First_Name, LastName = u2.Last_Name } : (Entity.Employee) null,
};
When I debug the query above, I get the following error:
Type of conditional expression cannot be determined because there is no implicit conversion between 'Employee' and 'Application'
How do I resolve this error?
It may not directly address an error you're getting, but you don't need that query at all, because you have navigation properties set. Use Include
instead and it should work just fine - EF will join necessary joins for you:
var query context.Applications
.Include("ModifiedBy")
.Include("CreatedBy")
.Where(a => a.ApplicationId == applicationId);
A couple things I am noticing
join u1 in context.Employees on a.CreatedBy equals u1.Employee_ID.Trim()
join u2 in context.Employees on a.ModifiedBy equals u2.Employee_ID.Trim()
You can't join like this, as CreateBy
and ModifiedBy
are of type Employee
, not string
Also, have a look at this:
(Entity.Employee) null
You can't cast null to Employee
. You might want to use the type's default value in future:
default(Entity.Employee)
UPDATE
As pointed out in the comments, it is legal to cast null to Entity.Employee
, but since you end up with null anyways there is not much point to the exercise. default(Entity.Employee)
also results in null, since that's the default value for reference types, but default
could provide a different value for various other types, which can sometimes be useful.
After some additional research, here is the revised code snippet that ended up working for me:
var result = (from a in context.Applications
join u1 in context.Employee on a.CreatedBy equals u1.Employee_ID.Trim()
join u2 in context.Employee on a.ModifiedBy equals u2.Employee_ID.Trim() into us
from u2 in us.DefaultIfEmpty()
where a.ApplicationId == applicationId
select new Entity.Application()
{
Id = a.ApplicationId,
Name = a.ApplicationName,
Description = a.ApplicationDesc,
DateTimeCreated = a.DateTimeCreated,
CreatedBy = new Entity.Employee
{
EmployeeID = a.CreatedBy,
FirstName = u1.First_Name,
LastName = u1.Last_Name
},
DateTimeModified = a.DateTimeModified ?? null,
ModifiedBy = new Entity.Employee
{
EmployeeID = a.ModifiedBy ?? string.Empty,
FirstName = u2.First_Name ?? string.Empty,
LastName = u2.Last_Name ?? string.Empty
}
}).Single();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.