Linq Query get preferred contact

Dinesh Bali
Dinesh Bali used Ask the Experts™
on
Hi,

Please advise what is wrong in my linq query. Also, please advise if there is any better way of doing below.
I need contact where preferredContactIndicator is true.
if preferredContactIndicator":true is not available then first array of contact needs to be available.

My C# code below
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;
                    }

Open in new window


JSON snippet below, removed unnecessary JSON to simplify:
{
   "contact":[
      {
         "contactNumberType":"HOME",
         "isdCode":"+86",
         "areaCode":"10",
         "contactNumber":"6755555",
         "preferredContactIndicator":true
      },
      {
         "contactNumberType":"MOBIL",
         "isdCode":"+86",
         "areaCode":"10",
         "contactNumber":"67029354"
      }
   ]
}

Open in new window


Error says
cont.contactNumberType;
 const does not exists.
Please correct my query.
Also, please advise if c# code can be written in better way

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Software Engineer
Top Expert 2009
Commented:
In C# LInq, you can not access any variable used in the Linq statement, in this case cont.
Code below just capture the first contact that has preferredContactIndicator = true
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;
}

Open in new window

Eduard GherguArchitect - Coder - Mentor

Commented:
Hi,
I think that the JSON payload should contain all the querycontact info, so the invocation of the .FirstOrDefault() extension method is not correct. In this scenario, it is required to iterate thru the items from querycontact and fill your objects properties.
Commented:
Along with what Miguel states, your LINQ is going to return an Enumerable of Contact, so your first logical if branch will lead to an exception since Enumerable does not have a property called contactNumberType.

Also, FirstOrDefault *is* correct assuming that there is only one preferredContactIndicator.

Proof of concept -
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} }}";
        }
    }
}

Open in new window

Produces the following output -Capture.PNG-saige-

Author

Commented:
Many Thanks for your help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial