Linq 搜索日期

发布于 2024-10-18 08:04:19 字数 507 浏览 2 评论 0原文

如何使用 Linq 进行搜索时使用日期。我想我在声明中遗漏了一些内容

string searchName = Request.Form["PersonName"];
DateTime searchDateFrom = Request.Form["ReceivedDateFrom"];
DateTime searchDateTo = Request.Form["ReceivedDateTo"];

var Results = (from va in _db.myTable
       where ((va.PersonName.Contains(searchName)
               && (va.ApplicationReceivedDate > searchDateFrom 
                   && va.ApplicationReceivedDate < searchDateTo)
               select va).ToList();

how do I used date on searching using Linq. I think I'm missing something on the declaration

string searchName = Request.Form["PersonName"];
DateTime searchDateFrom = Request.Form["ReceivedDateFrom"];
DateTime searchDateTo = Request.Form["ReceivedDateTo"];

var Results = (from va in _db.myTable
       where ((va.PersonName.Contains(searchName)
               && (va.ApplicationReceivedDate > searchDateFrom 
                   && va.ApplicationReceivedDate < searchDateTo)
               select va).ToList();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝天白云 2024-10-25 08:04:19

HttpRequest.Form 是一个 NameValueCollection,可以通过 int/string 获取其中的字符串索引器。也就是说,Request.Form["ReceivedDateFrom"] 返回一个字符串,您不能在不进行任何转换的情况下将其分配给类型为 DateTime 的变量。您可以尝试 DateTime.ParseExact< /a> 方法将字符串转换为 DateTime。但如果您不能保证字符串的格式正确,您可以使用 TryParse 方法。

HttpRequest.Form is a NameValueCollection, where you can get strings in it by int/string indexer. That is, Request.Form["ReceivedDateFrom"] returns a string, you can't assign it to a variable whose type is DateTime without any convert. You can try DateTime.ParseExact method to convert the string to a DateTime. But if you can't guarantee the string has a correct format, you can use a TryParse method.

痕至 2024-10-25 08:04:19

可能是一个拼写错误,但您需要将 searchDateFrom / searchDateTo 转换为 DateTime,并且 linq 语句中有两个额外的开括号

Might be a typo, but you need to cast searchDateFrom / searchDateTo to a DateTime and you have two extra open brackets in your linq statement

情绪操控生活 2024-10-25 08:04:19

我还建议使用更清晰的缩进,这样更容易跟踪和计算括号和其他内容。

string searchName = Request.Form["PersonName"];
DateTime searchDateFrom = Request.Form["ReceivedDateFrom"];
DateTime searchDateTo = Request.Form["ReceivedDateTo"];

var Results = (from va in _db.myTable
              where va.PersonName.Contains(searchName)
              && (va.ApplicationReceivedDate > searchDateFrom 
              && va.ApplicationReceivedDate < searchDateTo)
              select va).ToList();

I'd also recommand using a cleaner indentation, it's easier to follow and count the brackets and stuff.

string searchName = Request.Form["PersonName"];
DateTime searchDateFrom = Request.Form["ReceivedDateFrom"];
DateTime searchDateTo = Request.Form["ReceivedDateTo"];

var Results = (from va in _db.myTable
              where va.PersonName.Contains(searchName)
              && (va.ApplicationReceivedDate > searchDateFrom 
              && va.ApplicationReceivedDate < searchDateTo)
              select va).ToList();
天生の放荡 2024-10-25 08:04:19

这是我设置单元测试来查看发生了什么的时候。

检查括号并从字符串转换为日期时间:

[TestMethod]
    public void TestMethod1()
    {
        List<myTable> myTables = new List<myTable>();
        for (int month = 1; month < 10; month++)
        {
            for (int day = 1; day < 20; day++)
            {
                myTables.Add(new myTable { PersonName = "Person " + month.ToString() + " " + day.ToString(), ApplicationReceivedDate = new DateTime(2011, month, day) });
            }
        }

        string searchName = "Person";
        DateTime searchDateFrom = Convert.ToDateTime("2011-01-02");
        DateTime searchDateTo = Convert.ToDateTime("2011-01-03");

        var Results = (from va in myTables
                       where va.PersonName.Contains(searchName)
                               && va.ApplicationReceivedDate >= searchDateFrom
                                   && va.ApplicationReceivedDate < searchDateTo
                       select va);

        Assert.AreEqual(Results.Count(), 1);
    }

    public class myTable
    {
        public string PersonName { get; set; }
        public DateTime ApplicationReceivedDate { get; set; }
    }

还要检查 & 中的搜索到。

This is when I setup a unit test to see what's going on.

Check your brackets and casting from string to datetime:

[TestMethod]
    public void TestMethod1()
    {
        List<myTable> myTables = new List<myTable>();
        for (int month = 1; month < 10; month++)
        {
            for (int day = 1; day < 20; day++)
            {
                myTables.Add(new myTable { PersonName = "Person " + month.ToString() + " " + day.ToString(), ApplicationReceivedDate = new DateTime(2011, month, day) });
            }
        }

        string searchName = "Person";
        DateTime searchDateFrom = Convert.ToDateTime("2011-01-02");
        DateTime searchDateTo = Convert.ToDateTime("2011-01-03");

        var Results = (from va in myTables
                       where va.PersonName.Contains(searchName)
                               && va.ApplicationReceivedDate >= searchDateFrom
                                   && va.ApplicationReceivedDate < searchDateTo
                       select va);

        Assert.AreEqual(Results.Count(), 1);
    }

    public class myTable
    {
        public string PersonName { get; set; }
        public DateTime ApplicationReceivedDate { get; set; }
    }

Also check the search from & to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文