Dynamics 365 & Lambda Snippets

Summary

Here is a quick list of most used Lambda functions in Dynamics 365 CE.

// Loop through a list of collections to return a dependent component type that is an OptionSet with a value of 29
foreach (var result in response.EntityCollection.Entities.Where(x => x.GetAttributeValue("dependentcomponenttype").Value == 29))
{
    // Business Logic
}

x.GetAttributeValue<string>("name") == "Adam"))
{
    // Business Logic
}
// This query gets one permissible value for this entity and field.
 var actualValue = _context.CreateQuery("stringmap")
     .Where(x => x.GetAttributeValue("attributename") == "new_accreditationstatus")
     .Where(x => x.GetAttributeValue("value") == "7")
     .Where(x => x.GetAttributeValue("objecttypecode") == Account.EntityTypeCode)
     .Select(x => x.GetAttributeValue("value"))
     .Single();
// Sort an entity collection
var sortedList = EntityServiceCalls._allWorkflowCollection.ToList().OrderBy(x => x.GetAttributeValue("primaryentity"));
   EntityCollection entityCollection = service.RetrieveMultiple(new FetchExpression(fetch));

            foreach (var c in entityCollection.Entities.Where(x => (string)x.Attributes["logicalname"] == "contact"))
            {
                _GlobalEntityCol.Add(c);
            }