我定义了下面的实体类为我的LINQ查询的隐式转换:C#LINQ查询 - 错误
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; }
}
我创建了下面的查询来创建一个Application
对象,并试图以创建一个Employee
实体CreatedBy
和'ModifiedBy'属性。有时,ModifiedBy
列可以包含空值,我想将ModifiedBy
属性设置为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,
};
当调试上面的查询,我得到以下错误:
Type of conditional expression cannot be determined because there is no implicit conversion between 'Employee' and 'Application'
如何解决这个问题?
什么字段/属性'ApplicationId' - 应该说是刚刚'Id'? –
因此,上面的查询工作,如果你删除这一行? 'ModifiedBy =(a.ModifiedBy!= null)?新的Entity.Employee {EmployeeID = a.ModifiedBy,FirstName = u2.First_Name,LastName = u2.Last_Name} :(Entity.Employee)null,' – Nate
@nate - 是的,当我移除引用的行时,你的评论。 –