C# 无法将类型“ListName[]”隐式转换为“System.Collections.Generic.IList”

C# 无法将类型“ListName[]”隐式转换为“System.Collections.Generic.IList”,c#,asp.net,wcf,C#,Asp.net,Wcf,我有一个具有某些属性的IList。 我从数据库访问一组值,然后返回IList的代码。 我使用了一个Web服务,它将完整的细节提供给列表。 作为WCF的服务在WCFTestClient.exe中执行良好。 然而,在codebehind中,它在放置时显示错误 public IList<BrandInfo> SearchProduct(string name) { AuthenicationServiceClient obj = new AuthenicationServiceCl

我有一个具有某些属性的IList。 我从数据库访问一组值,然后返回IList的代码。 我使用了一个Web服务,它将完整的细节提供给列表。 作为WCF的服务在WCFTestClient.exe中执行良好。 然而,在codebehind中,它在放置时显示错误

public IList<BrandInfo> SearchProduct(string name)
{
    AuthenicationServiceClient obj = new AuthenicationServiceClient();
    return obj.SearchProducts(name); 

}
它显示了一个错误,无法将类型“Model.BrandInfo[]”隐式转换为“System.Collections.Generic.IList”

Web服务中的代码是

public IList<BrandInfo> GetBrandByQuery(string query)
{
    List<BrandInfo> brands = Select.AllColumnsFrom<Brand>()
        .InnerJoin(Product.BrandIdColumn, Brand.BrandIdColumn)
        .InnerJoin(Category.CategoryIdColumn, Product.CategoryIdColumn)
        .InnerJoin(ProductPrice.ProductIdColumn, Product.ProductIdColumn)
        .Where(Product.EnabledColumn).IsEqualTo(true)
        .And(ProductPrice.PriceColumn).IsGreaterThan(0)
        .AndExpression(Product.Columns.Name).Like("%" + query + "%")
        .Or(Product.DescriptionColumn).Like("%" + query + "%")
        .Or(Category.CategoryNameColumn).Like("%" + query + "%")
        .OrderAsc(Brand.NameColumn.ColumnName)
        .Distinct()
        .ExecuteTypedList<BrandInfo>();

    // Feed other info here
    // ====================
    brands.ForEach(delegate(BrandInfo brand)
    {
        brand.Delivery = GetDelivery(brand.DeliveryId);
    });

    return brands;
}
如何从客户端访问此代码。我无法为此提取任何相关的在线参考资料

您可以使用ToList方法:

请记住,这需要在文件顶部使用System.Linq

或者,您可以更改WCF配置,将集合反序列化为列表而不是数组

如果使用“添加服务引用”,则应执行以下操作:

右键单击服务引用并选择配置服务引用。 在“集合类型”下拉列表中,选择正确的类型:类似System.Collections.Generic.List的类型
我从您的错误消息中注意到的一点是,它清楚地指出:

无法将类型“Model.BrandInfo[]”隐式转换为“System.Collections.Generic.IList”

Model.BrandInfo与单独项目中定义的Models.BrandInfo不同。编译器不会以这种方式判断等价性。您必须在一个项目中声明它并在另一个项目中引用它,或者您必须自己编写映射程序

差不多

public IList<BrandInfo> SearchProduct(string name)
{
    AuthenicationServiceClient obj = new AuthenicationServiceClient();
    return obj.SearchProducts(name).Select(Convert).ToList(); 
}

public Models.BrandInfo Convert(Model.BrandInfo x)
{
    //your clone work here. 
}

或者,您应该尝试一些自动化这种映射的库,如或

解决此问题的一种方法是在调用结束时添加.ToArray-obj.SearchProductsname.ToArray;由于您似乎正在使用服务引用,您可以在添加引用时将其设置为“使用列表”,尽管我必须这样做是为了告诉您如何尝试所有可能性。但是显示error@tim我从错误消息中注意到的一件事是,它清楚地表明:无法将type“Model.BrandInfo[]”隐式转换为“System.Collections.Generic.IList”,请查看模型之间的差异。和模型。这两者有联系吗?是否存在名称空间不匹配?这两个是分开定义的吗?注意:GetBrandByQuery中的循环可以更简洁地写为brands{…}中的foreach BrandInfo brand。@nawfal是的,它们都位于不同的项目中,但BrandInfo的结构相同。向其中添加ToList后,它显示以下消息无法将类型“System.Collections.Generic.List”隐式转换为“System.Collections.Generic.List”,此处BrandInfo类被复制粘贴到其他项目中。是的,我可以在方法convert中显式转换。手动绑定到客户端所需的项。
public IList<BrandInfo> SearchProduct(string name)
{
    AuthenicationServiceClient obj = new AuthenicationServiceClient();
    return obj.SearchProducts(name).Select(Convert).ToList(); 
}

public Models.BrandInfo Convert(Model.BrandInfo x)
{
    //your clone work here. 
}