无法将类型'ListName []'隐式转换为'System.Collections.Generic.IList <ListName>'

Shi*_*abh 3 c# asp.net wcf

我有一个具有某些属性的IList.我从数据库访问一组值返回IList的代码.我使用了一个webservice,它将完整的细节提供给列表.作为WCF的服务在WCFTestClient.exe中很好地执行.但是在代码隐藏中,放置时会显示错误.

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

}
Run Code Online (Sandbox Code Playgroud)

它显示错误" Cannot implicitly convert type 'Model.BrandInfo[]' to 'System.Collections.Generic.IList<Models.BrandInfo>'"

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;
}
Run Code Online (Sandbox Code Playgroud)

如何从客户端访问此代码.我无法提取任何相关的在线参考.

naw*_*fal 6

我从您的错误消息中注意到的一件事是它明确指出:

无法隐式转换'Model.BrandInfo[]''System.Collections.Generic.IList<Models.BrandInfo>'

Model.BrandInfoModels.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. 
}
Run Code Online (Sandbox Code Playgroud)

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

  • 是的,我能够在方法转换中显式转换.手动绑定到客户端所需的项目. (2认同)