0

I'm trying to call a web service, and hitting a road block.

Here's a snippet in which I call the said web service in a console application to test (this works):

        WSProxy.CancelOrderClient x = new CancelOrderClient(); 
        WSProxy.OrderCancelObject y = new OrderCancelObject(); 

        var z = new List<OrderNumberObject>();
        var p = new OrderNumberObject();
        p.OrderNumber = "100";
        z.Add(p);

        y.OrderNumberTbl = z.ToArray();
        y.StoreNumber = "700";

        var a = x.CancelOrder(y);
        Console.WriteLine(a.ExResultMessage.Message);
        Console.WriteLine(a.ExResultMessage.Code);
        Console.ReadLine();

I want to put this in a separate static method to which the inputs are a List<string> for order numbers and a string for StoreNumber.

Each OrderNumberObject has just one string property, which is the OrderNumber which is of type string.

Given this context, I don't know how to convert/cast the List<string> to a List<OrderNumberObject>, where OrderNumberObject has a property called OrderNumber.

How do we do that? Unfortunately I have no freedom to modify the said web service I'm trying to call.

Hopefully that explained what I'm trying to do. Thank you.

2 Answers 2

2

Since you added a linq tag try something like below:

ListOfStrings.Select(s => new OrderNumberObject() { OrderNumber=s}).ToList();

edit:

(from s in ListOfStrings
 select new OrderNumberObject() {OrderNumber = s}
).ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @seven21. What does this expression look like with LINQ query syntax?
2
        var SomeListOfStrings = new List<string>(); //The list of strings that you received from your service call or whatever. I just created it here, but you would have got it from somewhere... 

        var orderNumbers = new List<OrderNumberObject>();

        foreach (var str in SomeListOfStrings)
        {
            //I am assuming the order number is a string property. 
            orderNumbers.Add(new OrderNumberObject {OrderNumber=str});
        }

I dont believe you need to do any casting if the OrderNumberObject.OrderNumber is a string, given you are receiving a string and storing it as a string.

Comments

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.