using System.Collections.Generic;
public string hba_vehiclenumber { get; set; }
public int hba_year { get; set; }
public string hba_make { get; set; }
public class DynamicsVehicle
public string hba_accountid { get; set; }
public string hba_make { get; set; }
public string hba_model { get; set; }
public int? hba_odometer { get; set; }
public bool? hba_isleased { get; set; }
public class EntityReference
public System.Guid Id { get; set; }
public string Name { get; set; }
public Dictionary<string, object> Attributes { get; set; }
public static bool KeyExistsAndNonNull (Dictionary<string, object> dict, string key)
return dict.ContainsKey(key) && dict[key] != null;
public static DynamicsVehicle MapToDynamicsVehicle (XrmEntity xrmEntity)
xrmEntity.Attributes = (from kvp in xrmEntity.Attributes
select kvp).ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
var account = (EntityReference)(xrmEntity.Attributes.ContainsKey("hba_accountid") ? xrmEntity.Attributes["hba_accountid"] : null);
var dynamicsVehicle = new DynamicsVehicle ()
hba_accountid = account != null
hba_make = xrmEntity.Attributes.ContainsKey("hba_make")
? xrmEntity.Attributes["hba_make"].ToString()
hba_model = xrmEntity.Attributes.ContainsKey("hba_model")
? xrmEntity.Attributes["hba_model"].ToString()
if (xrmEntity.Attributes.ContainsKey("hba_isleased"))
dynamicsVehicle.hba_isleased = (bool)xrmEntity.Attributes["hba_isleased"];
if (xrmEntity.Attributes.ContainsKey("hba_odometer"))
dynamicsVehicle.hba_odometer = (int)xrmEntity.Attributes["hba_odometer"];
public static void Main()
string vehiclesJsonData = "[{\"hba_vehiclenumber\": \"123\", \"hba_year\": 2000, \"hba_make\": \"123\", \"hba_model\": \"123\"}, {\"hba_vehiclenumber\": \"234\", \"hba_year\": 2001, \"hba_make\": \"123\", \"hba_model\": \"123\"}, {\"hba_vehiclenumber\": \"345\", \"hba_year\": 2002, \"hba_make\": \"123\", \"hba_model\": \"123\"}]";
List<AriVehicle> ariVehicles = JsonConvert.DeserializeObject<List<AriVehicle>>(vehiclesJsonData);
Console.WriteLine("Type: " + ariVehicles.GetType());
foreach (var vehicle in ariVehicles)
Console.WriteLine("VehicleNumber: " + vehicle.hba_vehiclenumber);
Console.WriteLine("Year: " + vehicle.hba_year);
Console.WriteLine("Make: " + vehicle.hba_make);
Console.WriteLine("------------------------------");
XrmEntity dVehicle = new XrmEntity() {
Attributes = new Dictionary<string, object>() {
{ "hba_isleased", true },
{ "hba_odometer", 12345 },
{ "hba_accountid", new EntityReference () {
var mappedVehicle = MapToDynamicsVehicle(dVehicle);
Console.WriteLine("mappedVehicle: " + JsonConvert.SerializeObject(mappedVehicle));