How to get all the birthdays of today?

后端 未结 5 1858
南方客
南方客 2021-01-23 09:21

Does anyone know how to make a Linq query that gets all the birthdays of today? The code below doesn\'t work :

var getBirthdays = 
    orgContext.CreateQuery<         


        
相关标签:
5条回答
  • 2021-01-23 10:01

    You could fetch this info with a Query, if that is possible in your situation?

    //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

    0 讨论(0)
  • 2021-01-23 10:05

    Anytime a vendor writes a four part blog series on how to do something as simple as finding a birthday (as Microsoft did in 2007), you have to know this won't be simple. So far as I can tell, this hasn't updated since then.

    • Find contacts with upcoming birthdays
    • Find contacts with upcoming birthdays - Part 2
    • Find contacts with upcoming birthdays - Parts 3 and 4

    So you have limited options:

    1. Make new fields called something like new_birthmonth and new_birthday that's updated every time a contact is created or updated via a plugin, and then query on those int fields.
    2. Using Dynamic Linq, construct an OR clause in your WHERE clause that checks to see if the birthday falls in a reasonable range of years (say, 140 for the long-livers) (code below).
    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();
    
    0 讨论(0)
  • If c.BirthDate is nullable, you have to convert it to a datetime first:

    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();
    
    0 讨论(0)
  • 2021-01-23 10:08

    You can use QueryExpression (it works for Microsoft CRM Plugin)

    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); ;
        }
    
    0 讨论(0)
  • 2021-01-23 10:18

    I solved my problem based on the example of "Peter Majeed" and using "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();
    

    The above query gave me the correct result! Thx to all who helped me!

    0 讨论(0)
提交回复
热议问题