Linq查询以获取两个日期之间的数据

时间:2018-08-30 09:08:28

标签: c# asp.net linq

日期和时间另存为:1397/06/10 13:55:49

Linq查询为:

var showFamily = (from b in database.tbl_kharidnaghdi

                          join u in database.tbl_Customer on b.CustomerID_FK equals u.CustomerID
                          join c in database.tbl_User on u.UserID_FK equals c.UserID
                          orderby b.KharidnaghdiID descending

                          where u.UserID_FK == userid
                            && txtFamily.Text.Contains(u.Family)
                            &&string.Compare(b.Date, txtDate1.Text) <= 0  
                            && string.Compare(b.Date, txtDate2.Text) >= 0
                          select new
                          {
                              u.Name,
                              u.Family,
                              u.Mobile,
                              u.Price,
                              u.CustomerID,
                              b.KharidnaghdiID,
                              b.EtebarHadiye,
                              b.Cashier,
                              b.Date,
                          }).ToList();
        grdShowListUser.DataSourceID = null;
        grdShowListUser.DataSource = showFamily;

我认为日期应与时钟分开,例如:1397/06/10

2 个答案:

答案 0 :(得分:1)

由于您要比较日期值,因此有必要使用DateTime.ParseExact()方法从文本框的Text属性(即string属性)转换它们并使用CompareTo()在它们之间进行比较:

DateTime date1 = DateTime.ParseExact(txtDate1.Text, "yyyy/MM/dd hh:mm:ss", CultureInfo.InvariantCulture);
DateTime date2 = DateTime.ParseExact(txtDate2.Text, "yyyy/MM/dd hh:mm:ss", CultureInfo.InvariantCulture);

var showFamily = (from b in database.tbl_kharidnaghdi
                  join u in database.tbl_Customer on b.CustomerID_FK equals u.CustomerID
                  join c in database.tbl_User on u.UserID_FK equals c.UserID
                  orderby b.KharidnaghdiID descending
                  where u.UserID_FK == userid
                  && txtFamily.Text.Contains(u.Family)
                  && b.Date.CompareTo(date1.Date) <= 0 // date comparison here
                  && b.Date.CompareTo(date2.Date) >= 0
                  select new
                  {
                      u.Name,
                      u.Family,
                      u.Mobile,
                      u.Price,
                      u.CustomerID,
                      b.KharidnaghdiID,
                      b.EtebarHadiye,
                      b.Cashier,
                      b.Date,
                  }).ToList();

请注意,如果您使用的是LINQ to Entities,则可能不支持Date属性,可以使用DbFunctions.TruncateTime()或将其投影到LINQ to ToList()或{{1 }}。

答案 1 :(得分:0)

数据库中的数据为1397/06/10 13:55:49 txtdate中的数据是1397/06/10

我假设1397是年份(yyyy),06是月份(MM),而10是日期(dd)。而且Db中的数据也采用字符串格式

DateTime date1 = DateTime.ParseExact(txtDate1.Text, "yyyy/MM/dd", CultureInfo.InvariantCulture);
DateTime date2 = DateTime.ParseExact(txtDate2.Text, "yyyy/MM/dd", CultureInfo.InvariantCulture);


var showFamily = (from b in database.tbl_kharidnaghdi
                  join u in database.tbl_Customer on b.CustomerID_FK equals u.CustomerID
                  join c in database.tbl_User on u.UserID_FK equals c.UserID
                  orderby b.KharidnaghdiID descending
                  where u.UserID_FK == userid
                  && txtFamily.Text.Contains(u.Family)
                  && DateTime.ParseExact(b.Date, "yyyy/MM/dd hh:mm:ss", CultureInfo.InvariantCulture) < date1
                  && DateTime.ParseExact(b.Date, "yyyy/MM/dd hh:mm:ss", CultureInfo.InvariantCulture) >= date2
                  select new
                  {
                      u.Name,
                      u.Family,
                      u.Mobile,
                      u.Price,
                      u.CustomerID,
                      b.KharidnaghdiID,
                      b.EtebarHadiye,
                      b.Cashier,
                      b.Date,
                  }).ToList();

注意:如果结果为空,则请更改大于或小于条件的并检查