如何获得今天的所有生日? [英] How to get all the birthdays of today?

查看:107
本文介绍了如何获得今天的所有生日?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我收到如下错误:


条件无效,实体成员正在调用无效的
属性或方法。


提前谢谢!

解决方案

我根据Peter Majeed的例子解决了我的问题,并使用LinqKit

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

上述查询给了我正确的结果!所有帮助我的人都是Thx!


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<Contact>()
              .Where(c => c.BirthDate != null 
                          && c.BirthDate.Value.Month == DateTime.Now.Month).ToList();

I get an error like this:

"Invalid 'where' condition. An entity member is invoking an invalid property or method."

Thanks in advance!

解决方案

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!

这篇关于如何获得今天的所有生日?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆