c# LINQ查询-没有隐式转换错误



我已经为我的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对象,并尝试为CreatedBy和'ModifiedBy'属性创建Employee实体。有时,ModifiedBy列可以包含null,我想将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,
             };  

当我调试上面的查询时,我得到以下错误:

不能确定条件表达式的类型,因为有"雇员"one_answers"申请"之间没有隐式转换

如何解决此错误?

它可能不会直接解决你得到的错误,但你根本不需要那个查询,因为你已经设置了导航属性。使用Include代替,它应该工作得很好- EF将为您连接必要的连接:

var query context.Applications
                 .Include("ModifiedBy")
                 .Include("CreatedBy")
                 .Where(a => a.ApplicationId == applicationId);

我注意到几件事

 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()

不能这样连接,因为CreateByModifiedBy的类型是Employee,而不是string

还有,看看这个:

 (Entity.Employee) null

不能将null强制转换为Employee。您可能希望将来使用该类型的默认值:

 default(Entity.Employee)

正如在评论中指出的那样,将null转换为Entity.Employee是合法的,但由于您最终以null结束,因此没有太多的意义。default(Entity.Employee)也导致null,因为这是引用类型的默认值,但是default可以为各种其他类型提供不同的值,这有时是有用的。

经过一些额外的研究,这里是修改后的代码片段,最终为我工作:

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();

相关内容

  • 没有找到相关文章