2010-04-22 31 views
0

我有這樣的代碼:無法隱式轉換類型...到...問題

 public static IEnumerable<dcCustomer> searchCustomer(string Companyname) 
    { 
     TestdbDataContext db = new TestdbDataContext(); 


     IEnumerable<dcCustomer> myCustomerList = (from Customer res 
                in db.Customers 
                where res.CompanyName == Companyname 
                select res); 

     return myCustomerList; 



    } 

而且不管我嘗試我不斷收到轉換錯誤。

Error 1 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<ConnectionWeb.Customer>' to 'System.Collections.Generic.IEnumerable<ConnectionWeb.DAL.dcCustomer>'. An explicit conversion exists (are you missing a cast?) \\srv01\home$\Z****\Visual Studio 2008\Projects\ConnectionWeb\ConnectionWeb\DAL\dcCustomer.cs 63 20 ConnectionWeb 

我想嘗試讓myCustomerList將值保存在枚舉器中並將其返回。

回答

2

問題是你期望返回一個DAl.dcCustome R型,但LINQ語句返回ConnectionWeb.Customer型...

您可以通過改變解決這個問題:

IEnumerable<dcCustomer> myCustomerList = 
(from Customer res 
    db.Customers 
    where res.CompanyName == Companyname 
    select res); 

到:

IEnumerable<dcCustomer> myCustomerList = (from Customer res 
    in db.Customers 
    where res.CompanyName == Companyname 
    select new dcCustomer(){ 
    //set properties here... 
    }); 

HTH

+0

感謝您的回答! – Younes 2010-04-23 08:02:18

1

在我看來,db.Customers包含ConnectionWeb.Customer類型的對象,而不像您所假設的那樣包含ConnectionWeb.DAL.dcCustomer。

相關問題