var querycontacts =
from cont in profile.contact
where cont.preferredContactIndicator == true
select cont;
if (querycontacts != null)
{
member.ContactType = cont.contactNumberType;
member.CountryCode = cont.isdCode;
member.ContactNumber = cont.contactNumber;
}else if (profile.contact != null && profile.contact.Count > 0)
{
member.ContactType = profile.contact[0].contactNumberType;
member.CountryCode = profile.contact[0].isdCode;
member.ContactNumber = profile.contact[0].contactNumber;
}
{
"contact":[
{
"contactNumberType":"HOME",
"isdCode":"+86",
"areaCode":"10",
"contactNumber":"6755555",
"preferredContactIndicator":true
},
{
"contactNumberType":"MOBIL",
"isdCode":"+86",
"areaCode":"10",
"contactNumber":"67029354"
}
]
}
Do more with
var querycontact = (from cont in profile.contact
where cont.preferredContactIndicator == true
select cont).FirstOrDefault();
if (querycontact != null )
{
member.ContactType = querycontact.contactNumberType;
member.CountryCode = querycontact.isdCode;
member.ContactNumber = querycontact.contactNumber;
}else if (profile.contact != null && profile.contact.Count > 0)
{
member.ContactType = profile.contact[0].contactNumberType;
member.CountryCode = profile.contact[0].isdCode;
member.ContactNumber = profile.contact[0].contactNumber;
}
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
namespace EE_Q29165114
{
class Program
{
static string data = "{" +
"'contact':[{" +
"'contactNumberType':'HOME'," +
"'isdCode':'+86','areaCode':'10'," +
"'contactNumber':'6755555'," +
"'preferredContactIndicator':true" +
"},{" +
"'contactNumberType':'MOBIL'," +
"'isdCode':'+86'," +
"'areaCode':'10'," +
"'contactNumber':'67029354'" +
"}]}";
static void Main(string[] args)
{
var profile = JsonConvert.DeserializeObject<Profile>(data);
var preferred = (from contact in profile.Contacts
where contact.PreferredContactIndicator
select contact).FirstOrDefault();
var member = new MemberContact();
if (preferred != null)
{
member.ContactType = preferred.ContactNumberType;
member.CountryCode = preferred.IsdCode;
member.ContactNumber = preferred.ContactNumber;
}
Console.WriteLine(member);
Console.ReadLine();
}
}
class Profile
{
[JsonProperty("contact")]
public List<Contact> Contacts { get; set; }
}
class Contact
{
public string ContactNumberType { get; set; }
public string IsdCode { get; set; }
public string ContactNumber { get; set; }
public bool PreferredContactIndicator { get; set; }
}
class MemberContact
{
public string ContactType { get; set; }
public string CountryCode { get; set; }
public string ContactNumber { get; set; }
public override string ToString()
{
return $"{{ ContactType: {ContactType}, CountryCode: {CountryCode}, ContactNumber: {ContactNumber} }}";
}
}
}
Produces the following output -
Premium Content
You need an Expert Office subscription to comment.Start Free Trial