2014-01-21 49 views
0

我不明白如何解决此问题。我将需要您的帮助和解释如何解决它。 我用一个WebService来填充具有艺术家名字的DropDownList。
这里是代码behing。无法解析客户端对象的返回值WebService

private List<ArtistServiceReference.ArtistServiceClient> ArtistDetail() 
{ 
    ArtistServiceReference.ArtistServiceClient client = 
     new ArtistServiceReference.ArtistServiceClient(); 

    ArtistServiceReference.Artist[] artists = client.ArtistDetail(); 

    return artists.ToList(); <=== errror here 

无法隐式转换类型System.Collections.Generic.List<ArtistServiceReference.Artist>System.Collections.Generic.List<ArtistServiceReference.ArtistServiceClient>

这里是ArtistService.cs

public class ArtistService : IArtistService 
{ 
    public List<Artist> ArtistDetail() 
    { 
     using (ArtistDataContext db = new ArtistDataContext()) 
     { 
      return (from artist in db.Artists 

       select new Artist() 
       { 
        Id = artist.Id, 
        Artist_nom = artist.Artist_nom 
       }).ToList(); 
     } 
    } 
} 
+1

难道你不明白什么部分错误消息的? – SLaks

+0

我不明白为什么它不让我返回一个ToList()来填充我的下拉列表 – user3127986

+1

_Read the error message_。它告诉你到底你做错了什么。 – SLaks

回答

1

如果你希望你的客户端方法返回的Artist个列表,你为什么把它声明作为返回列表ArtistClient s?

以下应该解决您的问题:

private List<ArtistServiceReference.Artist> ArtistDetail() 
{ 
    ... 
    return artists.ToList(); 
} 

,或者甚至更优雅:

using YourNamespace.ArtistServiceReference; 

private List<Artist> ArtistDetail() 
{ 
    ... 
    return artists.ToList(); 
} 
+0

如果我执行了此更改,则在最后得到此错误})。错误不能隐式地将类型'System.Collections.Generic.List '转换为'System.Collections.Generic.List '如果我删除了ToList()我在选择错误时出现错误无法隐式将类型'System.Linq.IQueryable '转换为'System.Collections.Generic.List '。存在明确的转换(您是否缺少演员?) – user3127986

+0

听起来您有第二个名为'Artist'的类。在这种情况下,请使用具有完全限定名称的第一个版本。 – Heinzi

+0

该程序现在编译,但我得到了一个linq错误显式构造的实体类型'艺术家'在查询中是不允许的错误是在ArtistService.cs结束})。请参阅 – user3127986

1

你的方法,你的返回类型应该是ArtistServiceReference.Artist的列表,而不是一个列表ArtistServiceReference.ArtistServiceClient。您想通过使用ArtistServiceClient返回艺术家列表,但您没有返回客户端列​​表。

private List<ArtistServiceReference.Artist> ArtistDetail() 
{ 
    ArtistServiceReference.ArtistServiceClient client = 
     new ArtistServiceReference.ArtistServiceClient(); 

    var artists = client.ArtistDetail(); 

    return artists.ToList(); 
} 
+0

它让我编译,但我在linq得到一个错误显式的实体类型'艺术家'在查询中的结构不允许错误是在ArtistService.cs结束})。 – user3127986

+0

您需要实际执行服务呼叫。 client.ArtistDetail()是一种对象类型,据我所知,它实际上不是检索任何艺术家的服务。 –

+0

我遵循的例子是使用default.aspx.cs。我的网站有多个页面。我打电话的页面是Apointement.aspx.cs。这只在用户登录时才加载。要构建列表,应将我的客户ArtistDetail()移动到默认的page.aspx.cs? – user3127986

0

这里是解决方案:

代码behind.cs

private List<ArtistServiceReference.Artist> ArtistDetail() 
{ 
    ArtistServiceReference.ArtistServiceClient client = new 
    ArtistServiceReference.ArtistServiceClient(); 

    ArtistServiceReference.Voiture[] artists = client.ArtistDetail(); 

    return artists.ToList(); 
} 

ArtistService.cs

public class ArtistService : IArtistService 
{ 
    public List<Artist> ArtistDetail() 
    { 
     using (ArtistDataContext db = new ArtistDataContext()) 
     { 
      return db.Artists.ToList(); 
     } 
    } 
}