2016-01-26 308 views
0

我在.net中,此代碼,我得到這個錯誤:C#無法隱式轉換類型「System.Linq.IQueryable <<匿名類型:

C# Cannot implicitly convert type System.Linq.IQueryable<<anonymous type:.. 

有人可以幫我指點迷津?

IQueryable<Reservations> queryRes; 
queryRes = (from p in ctx.Reservations 
          join a in ctx.Employees on p.Id_employee equals a.Id 
          join c in ctx.Clients on p.Id_client equals c.Id 
          orderby p.Id 
          select new 
          { 
           p.Date, 
           p.Hour, 
           c.Name, 
           c.Surname, 
           p.Email, 
           p.Phone, 
           p.Service, 
           name1=a.Nume, 
           surname1=a.Prenume 
          }); 
reservationsView.Source = queryRes.ToList(); 
+0

CollectionViewSource reservationsView; reservationsView =((CollectionViewSource)(this.FindResource(「reservationsViewSource」))); – greeny

+1

您正投射到匿名類型中,而不是投射到「預訂」中。 – Crowcoder

回答

0

聲明select new { ...創建一個新的匿名類型。從Reservations中選擇一切,或者創建一個具有所需屬性的自定義類。

0

如前所述選擇保留,而不是創建匿名對象,

 IQueryable<Reservations> queryRes; 
     queryRes = (from p in ctx.Reservations 
        join a in ctx.Employees on p.Id_employee equals a.Id 
        join c in ctx.Clients on p.Id_client equals c.Id 
        orderby p.Id 
        select p); 
     reservationsView.Source = queryRes.ToList(); 
+0

然後如何從其他表格中顯示我需要的列? – greeny

+0

按照Rafael R.的建議, – Nantharupan

2

嘗試創建一個簡單的視圖模型與任何名字......「ReservationsView」讓您在創建域「選擇新的」 ...

public class ReservationsView 
    { 

     public System.DateTime Date{ get; set; }  
     public string Hour{ get; set; } 
     public string Name{ get; set; } 
     public string Surname{ get; set; } 
     public string Email{ get; set; } 
     public string Phone{ get; set; } 
     public string Email{ get; set; } 
     public string name1{ get; set; } 
     public string surname1{ get; set; } 

    } 

然後......

IQueryable<ReservationsView> queryRes; 
queryRes = (from p in ctx.Reservations 
          join a in ctx.Employees on p.Id_employee equals a.Id 
          join c in ctx.Clients on p.Id_client equals c.Id 
          orderby p.Id 
          select new ReservationsView 
          { 
           p.Date, 
           p.Hour, 
           c.Name, 
           c.Surname, 
           p.Email, 
           p.Phone, 
           p.Service, 
           name1=a.Nume, 
           surname1=a.Prenume 
          }); 

希望有所幫助。

+0

現在我在「p」的任意位置出現「Invalid initializer member declarator join」。 – greeny

相關問題