如何获得今天的所有生日?

时间:2012-04-13 14:28:47

标签: linq datetime dynamics-crm-2011

有谁知道如何制作获得今天所有生日的Linq查询?以下代码不起作用:

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

我收到这样的错误:

  

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

提前致谢!

6 个答案:

答案 0 :(得分:4)

任何时候供应商撰写关于如何做一件简单的事情的四部分博客系列(如微软在2007年所做的那样),你必须知道这并不简单。据我所知,从那时起,这一点没有更新。

所以你的选择有限:

  1. 创建名为new_birthmonthnew_birthday的新字段,每次通过插件创建或更新联系人时都会更新,然后查询这些int字段。
  2. 使用Dynamic Linq,在OR子句中构建一个WHERE子句,检查生日是否在合理的年份范围内(例如,140岁的长肝脏) (以下代码)。
  3. 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();
    

答案 1 :(得分: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();

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

答案 2 :(得分: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();

答案 3 :(得分: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>());
}

You may want to investigate the other operators for the filters + conditions

答案 4 :(得分:0)

orgContext.CreateQuery<Contact>().ToList<Contact>().Where...

答案 5 :(得分: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); ;
    }