Toni0123
Toni0123

Reputation: 109

Get CRM contact from Dynamics using contains clause in Linq

I am trying to get contact from CRM using contains clause, but it does not work. I have done it using .Equals(), it works, but it is not very handy.

 using (OrganizationService service = new OrganizationService("ConnectionString"))
        {

            CrmOrganizationServiceContext context = new CrmOrganizationServiceContext(service);

            Contact contact = new Contact();


            var contactToReturn = from c in context.CreateQuery("contact")
                                  where c["fullname"].Equals(search)
                                  select c;

            foreach (Entity c in contactToReturn)
            {

                if (c.Contains("fullname") && c["fullname"] != null)
                    contact.ContactName = c["fullname"].ToString();

                if (c.Contains("emailaddress1") && c["emailaddress1"] != null)
                    contact.EmailAddress = c["emailaddress1"].ToString();
                else
                    contact.EmailAddress = "N/A";

                if (c.Contains("telephone1") && c["telephone1"] != null)
                    contact.Telephone = c["telephone1"].ToString();
                else
                    contact.Telephone = "N/A";

            }
            return contact;
        }

Upvotes: 0

Views: 342

Answers (1)

Timur U
Timur U

Reputation: 61

Try like that:

var contactToReturn = from c in context.CreateQuery("contact")
                                  where (c["fullname"] as string).Contains(search)
                                  select c;

Upvotes: 1

Related Questions