1

有谁知道如何进行获取今天所有生日的 Linq 查询?下面的代码不起作用:

var getBirthdays = 
    orgContext.CreateQuery<Contact>()
              .Where(c => c.BirthDate != null 
                          && c.BirthDate.Value.Month == DateTime.Now.Month).ToList();

我收到这样的错误:

“'where' 条件无效。实体成员正在调用无效的属性或方法。”

提前致谢!

4

5 回答 5

4

每当供应商编写一个由四部分组成的博客系列,介绍如何做一些简单的事情,比如寻找生日(就像微软在 2007 年所做的那样),你必须知道这并不简单。据我所知,从那以后就没有更新了。

所以你的选择有限:

  1. 每次通过插件创建或更新联系人时,创建名为类似的新字段new_birthmonth,然后在这些字段上进行查询。new_birthdayint
  2. 使用Dynamic LinqOR在您的子句中构造一个子句WHERE,检查生日是否在合理的年份范围内(例如,140 代表长寿)(下面的代码)。
List<string> birthdays = new List<string>(); //will contain list of OR clauses

//makes sure no CRM unsupported dates are passed (less than 1/1/1900)
for (int i = Math.Min(140, DateTime.Today.Year - 1900); i > -1; i--) 
{
    //adds a different date per year
    birthdays.Add
    (
        string.Format
        (
            //DateTimes are stored in UTC
            "BirthDate = DateTime.Parse(\"{0}\")",
            DateTime.Today.ToUniversalTime().AddYears(-i)
        )
    );
}

//completes the correct dynamic linq OR clause
string birthdayList = string.Join(" OR ", birthdays);

var getBirthdays = orgContext.CreateQuery<Xrm.Contact>()
    .Where(c => c.BirthDate != null)
    .Where(birthdayList)
    .ToList();
于 2012-04-13T18:09:43.343 回答
2

我基于“Peter Majeed”的示例并使用“LinqKit”解决了我的问题!

var predicate = PredicateBuilder.False<Contact>();
for (int i = Math.Min(140, DateTime.Today.Year - 1900); i > -1; i--)
{
    DateTime cleanDateTime = new DateTime(DateTime.Today.AddYears(-i).Year, DateTime.Today.AddYears(-1).Month, DateTime.Today.AddYears(-i).Day);
    predicate = predicate.Or(p => p.BirthDate == cleanDateTime.ToUniversalTime());
}
var getBirthdays = (from c in orgContext.CreateQuery<Contact>().AsExpandable().Where(predicate)
                     select c).ToList();

上面的查询给了我正确的结果!感谢所有帮助过我的人!

于 2012-04-16T14:38:29.360 回答
1

如果 c.BirthDate 可以为空,则必须先将其转换为日期时间:

var getBirthdays = orgContext.CreateQuery<Contact>()
                             .Where(c => c.BirthDate != null && 
                                     (Convert.ToDateTime(c.BirthDate).Month == 
                                        DateTime.Now.Month) && 
                                      Convert.ToDateTime(c.BirthDate).Day == 
                                        DateTime.Now.Day))
                             .ToList();
于 2012-04-13T15:18:33.290 回答
1

如果您的情况可能,您可以使用查询来获取此信息?

//set up the condition + filter
var ce = new Microsoft.Xrm.Sdk.Query.ConditionExpression();
ce.Operator = Microsoft.Xrm.Sdk.Query.ConditionOperator.LastXDays;
ce.AttributeName = "birthdate";
ce.Values.Add(30);

var fe = new Microsoft.Xrm.Sdk.Query.FilterExpression();
fe.AddCondition(ce);

//build query
var query = new Microsoft.Xrm.Sdk.Query.QueryExpression();
query.EntityName = "contact";
query.Criteria.AddFilter(fe);

//get results
var results = CrmHelperV5.OrgProxy.RetrieveMultiple(query);

//if you want early bound entities, convert here.
var contacts = new List<Contact>();
foreach(var result in results.Entities)
{
    contacts.Add(result.ToEntity<Contact>());
}

您可能需要调查过滤器 + 条件的其他运算符

于 2012-04-13T18:53:27.513 回答
0

您可以使用 QueryExpression(它适用于 Microsoft CRM 插件)

public EntityCollection getBirthdateList(IOrganizationService orgsService)
    {
        List<string> birthdays = new List<string>(); 

        //makes sure no CRM unsupported dates are passed (less than 1/1/1900)
        for (int i = Math.Min(140, DateTime.Today.Year - 1930); i > -1; i--)
        {
            //adds a different date per year
            birthdays.Add
            (
                DateTime.Now.AddYears(-i).ToString("yyyy-MM-dd")
            ); 
        }

       
        // Instantiate QueryExpression 
        var query = new QueryExpression("contact");

        // Define filter QEquote.Criteria
        var queryfilter = new FilterExpression();
        query.Criteria.AddFilter(queryfilter);

        // Define filter 
        queryfilter.FilterOperator = LogicalOperator.Or;
        queryfilter.AddCondition("birthdate",ConditionOperator.In,birthdays.ToArray());
        return orgsService.RetrieveMultiple(query); ;
    }
于 2020-12-09T04:50:10.590 回答