简体   繁体   English

拨打function里面linq查询

[英]Call function inside linq query

I have list of object and i want to filter this to between to date.我有 object 的列表,我想将其过滤到迄今为止。

I write function for this if date is between to dates return true else return false.如果日期介于日期之间,我为此写 function 返回 true,否则返回 false。 This is my function.这是我的 function。

private bool IsDateBetween(DateTime date, string min, string max) {
        bool resultMin = true;
        bool resultMax = true;
        if (!string.IsNullOrEmpty(min))
        {
            resultMin = date > Convert.ToDateTime(min);
        }
        if (!string.IsNullOrEmpty(max)) {
            resultMax = date < Convert.ToDateTime(max);
        }
        return resultMin&&resultMax;
    }

and this is how i call function with linq.这就是我用 linq 调用 function 的方式。

var policiesT = policies.Where(x=>IsDateBetween(x.StartofInsurance,startDateS,startDateF));

startDateS and startDateF is string of datetime and i checked that values. startDateS 和 startDateF 是日期时间字符串,我检查了该值。
They have value when i run the code.当我运行代码时,它们具有价值。
But problem is when i debug the function min and max values always coming null. This is the image before the calling function但问题是当我调试 function 最小值和最大值时总是出现 null。这是调用 function 之前的图像在此处输入图像描述 This is the image when function is running.这是function运行时的画面。 在此处输入图像描述 Why that values caming null can u helpp me.为什么这个值即将到来 null 你能帮助我吗?
Thanks谢谢

This maybe not an answer but in comments would be a mess.这可能不是答案,但在评论中会一团糟。 This sample works fine for me:这个样本对我来说很好用:

void Main()
{
    var policies = new List<Policy> {
        new Policy {StartofInsurance=new DateTime(2021,1,1)},
        new Policy {StartofInsurance=new DateTime(2021,6,21)},
        new Policy {StartofInsurance=new DateTime(2021,7,13)},
    };
    string startDateS = "01/09/2021";
    string startDateF = "07/01/2021";
    var policiesT = policies.Where(x => IsDateBetween(x.StartofInsurance, startDateS, startDateF));
    foreach (var p in policiesT)
    {
        Console.WriteLine(p.StartofInsurance);
    }
}

private bool IsDateBetween(DateTime date, string min, string max)
{
    bool resultMin = true;
    bool resultMax = true;
    if (!string.IsNullOrEmpty(min))
    {
        resultMin = date > Convert.ToDateTime(min);
    }
    if (!string.IsNullOrEmpty(max))
    {
        resultMax = date < Convert.ToDateTime(max);
    }
    return resultMin && resultMax;
}

public class Policy
{
    public DateTime StartofInsurance { get; set; }
}

And returns the correct policy:并返回正确的策略:

6/21/2021 12:00:00 AM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM