2014-03-12 173 views
2

我定義了下面的實體類爲我的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'

如何解決這個問題?

+0

什麼字段/屬性'ApplicationId' - 應該說是剛剛'Id'? –

+0

因此,上面的查詢工作,如果你刪除這一行? 'ModifiedBy =(a.ModifiedBy!= null)?新的Entity.Employee {EmployeeID = a.ModifiedBy,FirstName = u2.First_Name,LastName = u2.Last_Name} :(Entity.Employee)null,' – Nate

+0

@nate - 是的,當我移除引用的行時,你的評論。 –

回答

0

經過一些額外的研究,這裏是修改後的代碼片段,最終爲我工作:

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

它可能不會直接解決您收到的錯誤,但根本不需要該查詢,因爲您已設置導航屬性。使用Include,而不是它應該只是罰款 - EF將加入必要加入你:

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

當我嘗試查看此查詢的結果視圖時,我在調試模式下收到以下消息:兒童無法評估。 –

2

有兩件事情我注意到

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

你無法加入這個樣子,因爲CreateByModifiedByEmployee型的,而不是string

而且,看看這個:

(Entity.Employee) null 

您不能將null轉換爲Employee。你可能想在將來使用該類型的默認值:

default(Entity.Employee) 

UPDATE

正如在評論中指出,這是合法的投空到​​,但因爲你最終與空反正這個練習沒有多大意義。 default(Entity.Employee)也會導致爲null,因爲這是參考類型的默認值,但default可以爲各種其他類型提供不同的值,有時可能會有用。

+0

我同意'join',但你絕對可以將'null'投給'Entity.Employee'。 – MarcinJuraszek

+0

確實!我必須從我的C#前的日子裏帶出一些假設! –

+0

上下文應用程序表中的'CreatedBy'和'ModifiedBy'列被定義爲字符串。我正在進行連接以從「Employee」視圖的上下文獲取員工信息 –