Sinya
Sinya

Reputation: 154

Return List<string[]> and not string[][]

I have a WCF service that should return a list of string arrays:

[OperationContract]
List<string[]> ArticleSearch(int SearchField, string SearchValueMin,
           string SearchValueMax, IEnumerable<string> Fields);

I know that I can change the deserialization of lists from System.Array to System.Collections.Generic.List but it's useless to me because I either get string[][] (jagged array) or List<List<string>> (List of List of string) as return type. I need List<string[]>.

My question is; is it possible to configure this somewhere or do I have to change the Reference.cs file by hand, after every update?

Upvotes: 1

Views: 1517

Answers (2)

Franck
Franck

Reputation: 4440

Ultimately you can create a [DataContract] class with a List property and make your [OperationContract] return that object. at that point when you attached yourself to the WCF service from any .NET project it will be able to see the property as List.

But this is really not a good way to do it if it's just to receive List on the client side. @Corak comment to your post is the best way to do this. Don't forget other languages can read your service too and in the end all they see is XML (or json if you change default return type) .NET is just intepreting the XML to be a "Loadable" into a known object.

Upvotes: 0

Damith
Damith

Reputation: 63105

Normally collections types are returns as Arrays but you can change this behavior by configuring service reference as below.

select your service reference and go to Configure Service Reference...

In the Collection Type drop-down, select the type System.Collections.Generic.List

enter image description here

since you have return type as List<string[]> after above change you will receive List<List<string>>

So you can't have Array of List or List of Arrays at ones.

For your requirement you can convert array to List or List to array as Corak commented.

Upvotes: 5

Related Questions