Невозможно неявно преобразовать тип ... в ... проблему - PullRequest
0 голосов
/ 22 апреля 2010

У меня есть этот код:

       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 ]

2 голосов
/ 22 апреля 2010

проблема в том, что вы ожидаете вернуть тип DAl.dcCustome r, но оператор linq возвращает тип ConnectionWeb.Customer ... ... 1003 *

Вы можете преодолеть это, изменив:

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...
  });

НТН

1 голос
/ 22 апреля 2010

Похоже, db.Customers содержит объекты типа ConnectionWeb.Customer, а не ConnectionWeb.DAL.dcCustomer, как вы предполагаете.

...