Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Answered

Linq Query. the given key was no present

Posted on by 110

Hello,

Very new to c Sharp and trying to query CRM 365 CE and I noticed that if a field comes back as blank or null it gives me this error?

In general how do you check for this and it's normal to have lots of nulls on any of the fields how does one handle that?

using (OrganizationServiceContext context = new OrganizationServiceContext(service))
{
// Pull contacts
var query = from account in context.CreateQuery("account")
join contact in context.CreateQuery("contact")
on account["primarycontactid"] equals contact["contactid"]
// where account["address1_city"] == "Woodland Hills" //&& account["name"] == "Peak Financial"

//where contact["fullname"] == "Dan Russ"
select new
{
Name = account["name"],
//Email = (contact["emailaddress1"] != null && contact["emailaddress1"] != ""),
// fax =! contact.Contains("fax"),
// fax = account["fax"],
CName = contact["fullname"],
CEmail = contact["emailaddress1"],
Manager = contact
};


foreach (var account in query)
{


//service.Update()
//Console.WriteLine(account.Name + " is run by: " + account.Manager["fullname"] + " Email " + account.Email + " fax " + account.fax);

Console.WriteLine(account.Name);

Console.WriteLine(account.CName + " " + account.CEmail);

}

Console.Read();

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Linq Query. the given key was no present

    Hello,

    Try to use GetAttributeValue<Type>("field name") to resolve your issue instead of ["field name"] like:

    where account.GetAttributeValue<string>("address1_city") == "Woodland Hills"

    instead of

    where account["address1_city"] == "Woodland Hills"

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans