You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用C#工作流向Dynamics 365 CRM所有联系人发送生日邮件?

Fixing the LINQ Query Error for Birthday Contacts in Dynamics 365 CRM

Hey there! Let's get that birthday email query sorted out. The error you're seeing happens because the Dynamics 365 LINQ provider can't handle accessing the Value property of a nullable DateTime (like c.BirthDate.Value.Month) directly in your where clause—it doesn't know how to translate that into valid FetchXML (the query language CRM uses under the hood).

The Problem with Your Original Code

Your original code tries to check the month and day of the BirthDate by accessing Value, but CRM's LINQ provider doesn't support this. Instead, we need to use CRM-specific functions to extract the month and day from the BirthDate field.

Corrected LINQ Query

Here's the fixed version using QueryFunctions (from the Microsoft.Xrm.Sdk.Linq namespace) to safely get the month and day values:

using Microsoft.Xrm.Sdk.Linq;

// No need to subtract 90 years—we just need today's month and day
var today = DateTime.Today;

var contacts = from c in orgContext.CreateQuery<Contact>()
               where c.BirthDate != null 
               && QueryFunctions.Month(c.BirthDate) == today.Month
               && QueryFunctions.Day(c.BirthDate) == today.Day
               select new { c.Id, c.LogicalName, c.BirthDate, c.FullName };

Alternative: Using QueryExpression

If you prefer (or run into more LINQ limitations), you can use QueryExpression instead—it's often more flexible for CRM queries:

var today = DateTime.Today;

var birthdayQuery = new QueryExpression(Contact.EntityLogicalName)
{
    ColumnSet = new ColumnSet("contactid", "birthdate", "fullname"),
    Criteria = new FilterExpression(LogicalOperator.And)
    {
        Conditions = 
        {
            new ConditionExpression("birthdate", ConditionOperator.NotNull),
            new ConditionExpression("birthdate", ConditionOperator.MonthEqual, today.Month),
            new ConditionExpression("birthdate", ConditionOperator.DayEqual, today.Day)
        }
    }
};

// Convert the results to your desired anonymous type
var contacts = orgContext.RetrieveMultiple(birthdayQuery)
                         .Entities
                         .Select(e => new 
                         {
                             Id = e.Id,
                             LogicalName = e.LogicalName,
                             BirthDate = e.GetAttributeValue<DateTime?>("birthdate"),
                             FullName = e.GetAttributeValue<string>("fullname")
                         });

Why This Works

  • QueryFunctions.Month and QueryFunctions.Day are recognized by the CRM LINQ provider and get translated into valid FetchXML conditions.
  • The QueryExpression approach uses CRM's built-in MonthEqual and DayEqual operators, which are designed exactly for this scenario—finding records where a date field matches a specific month/day regardless of year.

Just make sure you have the Microsoft.Xrm.Sdk and Microsoft.Xrm.Sdk.Linq NuGet packages installed, and your orgContext is a properly initialized OrganizationServiceContext instance.

内容的提问来源于stack exchange,提问作者user9261862

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:23:03