我定義了下面的實體類爲我的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 - 是的,當我移除引用的行時,你的評論。 –