无法隐式转换类型...到...问题

时间:2010-04-22 13:14:11

标签: c# .net enumeration

我有这段代码:

       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 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

在我看来,db.Customers包含ConnectionWeb.Customer类型的对象,而不是像你假设的ConnectionWeb.DAL.dcCustomer。