Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

asp.net

wcf

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.

like image 408
Shiva Saurabh Avatar asked Mar 23 '26 21:03

Shiva Saurabh


2 Answers

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

like image 133
nawfal Avatar answered Mar 25 '26 11:03

nawfal


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
like image 41
MarcinJuraszek Avatar answered Mar 25 '26 09:03

MarcinJuraszek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!