无法将类型...隐式转换为...问题

发布于 2024-08-30 01:20:37 字数 981 浏览 1 评论 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 将值保留在枚举器中并返回它。

I have this code:

       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;



    }

And whatever i try i keep getting the convert error.

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

I want to try get myCustomerList to keep the values in an enumerator and return it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷到爆炸 2024-09-06 01:20:37

问题是您期望返回 DAl.dcCustomer 类型,但 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

the problem is the you are expecting to return a DAl.dcCustomer type, but the linq statement is returning a ConnectionWeb.Customer type...

You could overcome this by changing:

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

to:

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

HTH

空城旧梦 2024-09-06 01:20:37

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

Looks to me like db.Customers contains objects of type ConnectionWeb.Customer and not ConnectionWeb.DAL.dcCustomer like you assume.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文