0

I am writing a wrapper around web services by third party. The interface looks like this:

public System.Data.DataSet GetXxxx(string functionName, object[][] parameters)

Some of the functionNames use no parameters, some of them use one, some of them more, like this (from their doc):

parameters[0][0]: @key_one
parameters[0][1]: value for @key_one
parameters[1][0]: @key_two
parameters[1][1]: value for @key_two

To generalize the wrapper, I was thinking about passing Hashtable or Dictionary<string, object>, and to create the parameters[][] inside the wrapper dynamically.

How can I do that?

Update: Actually, the parameter pairs have to keep the order. Do, I have to change the interface to something like:

using ListOfParamPairs = List<KeyValuePair<string, object>>;

...

public static DataSet GetXxxx(string functionName, ListOfParamPairs parameters)
{
    ...
}
2
  • 1
    If I uderstand you right; To var dictionary = parameters.ToDictionary(line => line[0].ToString(), line => line[1]);? Back: object[][] parameters = dictionary.Select(pair => new object[] {pair.Jey, pair.Value}).ToArray(); Commented Sep 27, 2018 at 8:37
  • Dictionary<K, V> doesn't have any order. If order of the pararmeters matters, yes, you have to switch to List<KeyValue<K, V>> or alike Commented Sep 27, 2018 at 10:00

1 Answer 1

3

If I understand you right, you can try Linq:

From object[][] to Dictinary<string, object>:

 using System.Linq;

 ...

 Dictionary<string, object> dictionary = parameters
 //.Where(line => line.Length == 2 && line[0] != null) // valid params only
   .ToDictionary(line => line[0].ToString(), 
                 line => line[1]);

Back to object[][]:

 using System.Linq;

 ...

 object[][] parameters = dictionary
   .Select(pair => new object[] {pair.Key, pair.Value})
   .ToArray();

Edit: In case we have to switch to List<KeyValuePair<String, Object>> (we want to preserve the initial order; dictionary's pairs are not in order):

To:

 List<<KeyValuePair<String, Object>> list = parameters
   //.Where(line => line.Length == 2 && line[0] != null) // valid params only  
   .Select(line => new KeyValuePair(line[0].ToString(), line[1]))
   .ToList();

Back: (technically, the same)

  object[][] parameters = list
    .Select(pair => new object[] {pair.Key, pair.Value})
    .ToArray();
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Dmitry. Actually, I have to preserve the order of the parameters. Can you comment on the update?
@pepr: If we switch from Dictinary<string, object> to List<<KeyValuePair<String, Object>> we have to modify To part a bit (see my edit, please)
In your opinion, is the List<<KeyValuePair<String, Object>> the way to go, or would you choose another way? (I am thinging about how the parameters are to be written explicitly when calling the warapper.)
@pepr: Well, List<KeyValue<string, object>> is quite good choice, providing that you don't have many (say, thousands) parameters which you address by their name. Possible (but in my opinion, worse) alternative is List<Tuple<string, object>>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.