Shiva Saurabh
Shiva Saurabh

Reputation: 1289

Cannot implicitly convert type 'ListName[]' to 'System.Collections.Generic.IList<ListName>'

I Have an IList that has certain properties. i access a set of values from database to the code which returns IList. I have used a webservice which gives the complete details into the list. the service being WCF gets executed well in WCFTestClient.exe. However in codebehind it is showing an error when placed.

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

}

it is showing an error "Cannot implicitly convert type 'Model.BrandInfo[]' to 'System.Collections.Generic.IList<Models.BrandInfo>'"

the code in the webservice being.

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

How can i access this code from the client side. I couldn't extract any relevant online reference for this.

Upvotes: 3

Views: 3837

Answers (2)

nawfal
nawfal

Reputation: 73283

One thing I notice from your error message is that it clearly states:

Cannot implicitly convert type 'Model.BrandInfo[]' to 'System.Collections.Generic.IList<Models.BrandInfo>'

Model.BrandInfo is different from Models.BrandInfo defined in separate projects. The compiler wouldn't make out the equivalence in such a manner. You have to either declare it in one project and reference it in the other, or you have to write a mapper yourself.

Something like

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

Or you should try some libraries that automate this mapping like AutoMapper or ValueInjecter

Upvotes: 6

MarcinJuraszek
MarcinJuraszek

Reputation: 125660

You could do that using ToList method:

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

Remember, that it would require using System.Linq at the top of the file.

Or you can change your WCF configuration to deserialize collection as lists instead of arrays.

If you use Add service reference you should do following:

  • Right click on the service reference and select Configure Service Reference.
  • In the Collection Type drop-down, select correct type: something like System.Collections.Generic.List

Upvotes: 4

Related Questions