0

I'm trying to create and consume a web service... I'm using .NET Framework 4.0 (c#). The service exposes a method like this:

  public List<object[]> GetData(string strRegion, List<string> lstBrand, List<string> lstColor)

Then on the client application, I declare a list of objects:

  List<object[]> lst = new List<object[]>();

... and attempt to fill it like so:

  MyService.MyClient os = new MyClient();
  lst = os.GetData(myRegionString, myBrandList, myColorList);

... but I get, "Cannot implicity convert type 'object[][]' to 'System.Collections.Generic.List'". What am I doing wrong?

Thanks!


Try

MyService.MyClient os = new MyClient();
  lst = os.GetData(myRegionString, myBrandList, myColorList).ToList();

Lists will get serialized into arrays as they are passed from the web service to the client, so you'll need to convert it back to a list again.

4

4 回答 4

3

In the solution explorer, right click your service and do configuration. You may have set the data collection default type to array. You can set it to be a list type. Also, I would rethink sending an object over a web service. You typically want a well defined data type. I don't think you can even send type object over a web service.

于 2012-09-17T19:16:32.800 回答
2

尝试

MyService.MyClient os = new MyClient();
  lst = os.GetData(myRegionString, myBrandList, myColorList).ToList();

列表在从 Web 服务传递到客户端时将被序列化为数组,因此您需要再次将其转换回列表。

于 2012-09-17T19:16:06.293 回答
0

The MyClient.GetData() method is returning a jagged array. To convert this array into a list, you should be able to use the ToList() method from the System.Linq namespace:

lst = os.GetData(myRegionString, myBrandList, myColorList).ToList();
于 2012-09-17T19:16:52.593 回答
0

Obviously you return a jagged array, not a generic list of object[]. One solution is already suggested - just change the type of the return value. But what I think is better is to use interfaces everywhere - both the array and the generic list implement IList, so if you make your method to return a value of type IList, and your lst variable is IList as well, then you shouldn't have problems even if your method returns a jagged array.

于 2012-09-17T21:26:21.517 回答