web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Linq Query. the given key was no present

(0) ShareShare
ReportReport
Posted on by 116

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();

I have the same question (0)
  • Verified answer
    a33ik Profile Picture
    84,331 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 258

#2
MVP-Daniyal Khaleel Profile Picture

MVP-Daniyal Khaleel 179

#3
Tom_Gioielli Profile Picture

Tom_Gioielli 127 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans