2

enter image description hereI have a keyValupair of Hotel details;:

//This is where the data comes from : -

   JavaScriptSerializer json_serializer = new JavaScriptSerializer();
            dynamic hotels1 = (dynamic)json_serializer.DeserializeObject(jso);
var keyValuePairs = hotels1["hotels"];

var hotelList = keyValuePairs["hotels"];   // hotelList[0]  ={'key'='Code' ;value='123'} 
                                           //{'key'='Name' 
                                           // ;value='Sheraton'}  

how do i convert this to a list of Hotel

List<Hotel> hotaals = new List<Hotel>();

where Hotel is

public class Hotel
{
    public int code { get; set; }
    public string name { get; set; }
}

I use a for loop to map fields, but my great boss says its inefficient and i have to use Linq.

The loop i use

    foreach (dynamic h in hotelList)
    {

        oneHotel = new Hotel();
        oneHotel.code = h["code"];
        oneHotel.name = h["name"];
  myHotels.Add(oneHotel);
 }
Olivier Rogier
  • 8,997
  • 4
  • 12
  • 26
  • Show how you get your kvp... – zaggler Oct 08 '19 at 21:38
  • Do you mean you have a `Dictionary` you need to convert to a `List`, where the memebers of the class don't match the KeyValuePair(s) of your Dictionary? About the loop being inefficient (...). – Jimi Oct 08 '19 at 21:42
  • 1
    Is a loop inefficient for the executing performance or speed of programming? If your "great boss" thinks smaller code means faster executing, he could be wrong. (https://stackoverflow.com/questions/1044236/nested-foreach-vs-lambda-linq-query-performancelinq-to-objects) – GvS Oct 08 '19 at 21:43
  • 1
    Please include an actual definition for `hotelList`. Pseudo-code and undefined code samples can make it hard to provide a detailed answer. – Rufus L Oct 08 '19 at 21:55
  • If you're already using a for loop, why not post the code? It would make the question very clear. – John Wu Oct 08 '19 at 22:04
  • I posted the kvp and the loop i use – Jayapen Jose Oct 08 '19 at 22:19
  • A little off topic but I utilize Json.NET library to interact with JSON from my javascript front end calls. It makes converting JSON into .NET objects a breeze. https://www.newtonsoft.com/json – nulltron Oct 08 '19 at 22:56

2 Answers2

1

So first you get your initial data:

var hotelList = keyValuePairs["hotels"];

Then use linq to create your new list:

var hotelObjects = hotelList.Select(hotel => new Hotel { code = hotel.key, name = hotel.name});

Now to be clear what linq is doing under the hood is an iterative loop through the objects (just like foreach) and creates a new Hotel object for each item in the hotelList and returns them as an IQueryable<Hotel>. Just apply .ToArray() or .ToList() if you don't want an IQueryable<>

Now from what it sounds like your initial List of hotel details isn't structured so you might have to modify my supplied linq query above to suit the structure of the list.

You may need something closer to this:

// Gives IQueryable<Hotel> as result
var hotelObjects = hotelList.Select(hotel => new Hotel{code = hotel["key"], name = hotel["name"]});

// Gives Array<Hotel> as result
var hotelObjects = hotelList.Select(hotel => new Hotel{code = hotel["key"], name = hotel["name"]}).ToArray();

// Gives List<Hotel> as result
var hotelObjects = hotelList.Select(hotel => new Hotel{code = hotel["key"], name = hotel["name"]}).ToList();
nulltron
  • 583
  • 1
  • 6
  • 23
1

Well the brute force way would be to just project the dictionary to objects by hard-coding the properties:

List<Hotel> hotaals = hotelList.Select(kvp => new Hotel {
                                    code = kvp['Code'],
                                    name = kvp["Name"]
                                    })
                               .ToList();

I would also challenge what your "great boss" means by inefficient".

D Stanley
  • 139,271
  • 11
  • 154
  • 219