简体   繁体   English

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

[英]C# LINQ query - No implicit conversion error

I have defined the following entity classes for my LINQ query: 我为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; }
}

I have created the following query to create an Application object and trying to create an Employee entity for the CreatedBy and 'ModifiedBy' properties. 我创建了以下查询来创建Application对象,并尝试为CreatedBy和'ModifiedBy'属性创建Employee实体。 Sometimes, the ModifiedBy column can contain null and I want to set the ModifiedBy property to null. 有时, 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,
             };  

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: 使用Include代替,它应该可以正常工作-EF将为您加入必要的联接:

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 您不能像这样加入,因为CreateByModifiedBy属于Employee类型,而不是string

Also, have a look at this: 另外,看看这个:

 (Entity.Employee) null

You can't cast null to Employee . 您不能将NULL强制转换为Employee You might want to use the type's default value in future: 您将来可能要使用该类型的默认值:

 default(Entity.Employee)

UPDATE 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. 如评论中所指出的,将NULL Entity.EmployeeEntity.Employee是合法的,但是由于无论如何您最终都会以null结束,因此该练习没有太多意义。 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. default(Entity.Employee)也将导致null,因为这是引用类型的默认值,但是default可以为其他各种类型提供不同的值,这有时会很有用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM