使用C#进行LINQ查询DateTime字段中的compareAge(或年份)(Age From) [英] calculateAge (or year) from LINQ Query DateTime field using C# for comparision (Age From)

查看:473
本文介绍了使用C#进行LINQ查询DateTime字段中的compareAge(或年份)(Age From)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参见在LINQ Query中将calculateAge转换为以年为单位的当前年龄(它适用于其他键,例如:EyeColor,但年龄转换失败),我想与Array中存储的值进行比较.我尝试将值存储在链接上方,"case"AgeTo":正下方的DateTime临时变量中,但是我无法采用x.profile.BirthDate值,因为仅在LINQ Query中允许使用它.看起来LINQ查询在构建过程中没有给出错误,但是,它无法配置

please see calculateAge to convert the Birthdate to current age in years in LINQ Query (it works for other keys, ex: EyeColor, but age conversion failing), I would like to compare with the value that was stored in Array. I tried storing the value in DateTime temp variable above the link, right below "case "AgeTo":" but I can not take the value x.profile.BirthDate as it's allowed only within LINQ Query. It appears that LINQ query is not giving error during build, however, it's failing to con

错误:

LINQ to Entities无法识别方法'Int32 computeAge(System.DateTime)'方法,并且该方法不能为 转换为商店表达式.

LINQ to Entities does not recognize the method 'Int32 calculateAge(System.DateTime)' method, and this method cannot be translated into a store expression.

任何帮助如何处理?

代码和calculateAge方法:

Code and calculateAge method:

if (searchList.Count > 0)
{
    foreach (KeyValuePair<String, String> kvp in searchList)
    {
        switch (kvp.Key)
        {
            case "AgeFrom":
                photosquery = photosquery.Where(x => calculateAge(Convert.ToDateTime(x.profile.BirthDate)) >= Convert.ToInt32(kvp.Value));
                break;
            case "AgeTo":
                photosquery = photosquery.Where(x => calculateAge(Convert.ToDateTime(x.profile.BirthDate)) <= Convert.ToInt32(kvp.Value));
                break;
            case "EyeColor":
                photosquery = photosquery.Where(x => x.physical.EyesColor.Contains(kvp.Value));
                break;
        }
    }
}

//My code for calculateAge:

public static int calculateAge(DateTime birthDay)
{
    int years = DateTime.Now.Year - birthDay.Year;

    if ((birthDay.Month > DateTime.Now.Month) || (birthDay.Month == DateTime.Now.Month && birthDay.Day > DateTime.Now.Day))
        years--;

    return years;
}

推荐答案

有一个更简单的解决方案.与其比较年龄,不如比较日期本身.

There's an easier solution. Rather than comparing the age, compare the dates themselves.

case "AgeFrom":
    var fromAge = Convert.ToInt32(kvp.Value);
    var maxDate = DateTime.Today.AddYears(-fromAge);
    photosquery = photosquery.Where(x => x.profile.BirthDate <= maxDate);
    break;
case "AgeTo":
    var toAge = Convert.ToInt32(kvp.Value);
    var minDate = DateTime.Today.AddYears(-toAge-1).AddDays(1);
    photosquery = photosquery.Where(x => x.profile.BirthDate >= minDate);
    break;

这具有使查询可保存的额外好处,因此它假设您已经在BirthDate字段上创建了一个索引,则可以利用索引.

This has the added benefit of making the query sargable, so it can make use of an index, assuming you've created one on the BirthDate field.

这篇关于使用C#进行LINQ查询DateTime字段中的compareAge(或年份)(Age From)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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