2

I'm working on the Array in C#, as following code below, the uricontent is a List in which each string contains one JSON value, I could parse the content, however, I want to have one other array or List to store each parsed value, in the example below, rooms variable can store each time one JSON parsed value, now I wish to store those parsed values in one array.

        int i = 0;

        while (uricontent.Count != 0)
        {
            var rooms = JObject.Parse(uricontent[i].ToString())
            ["rooms"]
            .Select(x => new
            {
                roomID = (string)x["room_name"],
                Name = WebUtility.HtmlDecode((string)x["room_name"]),
                Price = PriceHelper.Convert((string)x["discountedTotal"]),
                Currency = (string)x["currency"],
                Occupan = (int)x["adult"]
            }).ToArray();



            i++;
        }

rooms {<>f_AnonymousType11[1]<>f_AnonymousType11[]

[0] { roomID = "Superior 1st floor", Name = "Superior 1st floor", Price = 207.4, Currency = "EUR", Occupan = 2 }

As indicating above, the rooms overwrite the data in each iteration, how can I store those values in one other array like [1]..... [2]..... ....

Thanks

TrangZinita
  • 107
  • 1
  • 9

1 Answers1

0

I think what you need is the SelectMany method. SelectMany concatenates all of the IEnumerables generated by the inner Select statements, and returns them as a single IEnumerable, which can then be converted into an array:

var rooms = uricontent
    .SelectMany(
        uriContentElementJson =>
        {
            JObject uriContentElement = JObject.Parse(uriContentElementJson);
            return uriContentElement["rooms"].Select(
                room => new
                {
                    RoomID = (string)room["room_name"],
                    Name = WebUtility.HtmlDecode((string)room["room_name"]),
                    Price = PriceHelper.Convert((string)room["discountedTotal"]),
                    Currency = (string)room["currency"],
                    Occupant = (int)room["adult"]
                });
        })
    .ToArray();
Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
  • hi @ChrisMantle http://stackoverflow.com/questions/23015735/parsing-html-page-into-parent-child-object-c-sharp Could you have a look at this question link ? Im facing some selectmany stuffs too Thanks – TrangZinita Apr 11 '14 at 14:58