简体   繁体   English

C#Linq to SQL查询2日期之间否获得结果

[英]C# Linq to SQL Query between 2 Dates nos getting results

I'm building a complex search query using Textboxes, Comboboxes and DateTimePicker. 我正在使用Textboxes,Comboboxes和DateTimePicker构建一个复杂的搜索查询。

For the Textbox and Combobox, the queries are working fine, but when add a datetimepicker for query between 2 dates, I'm getting nothing... 对于文本框和组合框,查询工作正常,但是在两个日期之间添加datetimepicker进行查询时,我什么也没得到...

Using EF6 with LINQ... 将EF6与LINQ一起使用...

So, my class to store data: 所以,我的类来存储数据:

public class Projetos
    {
        public int ID { get; set; }
        public string Conc { get; set; } //Combobox
        public string Ent { get; set; } //Combobox
        public string Def { get; set; } //Textbox
        public DateTime DataR { get; set; }
    }

The first query and return results: 第一个查询并返回结果:

public IEnumerable<Proj> LoadProj()
    {
        var ctx = new JEntities();
        var query = (from p in ctx.tblProjs.AsQueryable()
                     join c in ctx.tblConcs on p.ConcID equals c.ConcID
                     join e in ctx.tblEnts on p.EntID equals e.EntID
                     select new Proj
                     {
                         ID = p.ProjID,
                         Conc = c.NameConc,
                         Ent = e.NameEnt,
                         Def = p.DefP,
                         DataR = p.DataR
                     });
        // The combination of these 3 controls works fine!           
        if (TxtDefP.TextLength > 0)
        {
            query = query.Where(s => s.DefP.Contains(TxtDefP.Text));
        }
        if(CmbCon.SelectedValue != null)
        {
            string SelectedValue = CmbConc.GetItemText(CmbConc.SelectedItem);
            query = query.Where(s => s.Conc == SelectedValue);
        }
        if(CmbEnt.SelectedValue != null)
        {
            string SelectedValue = CmbEnt.GetItemText(CmbEnt.SelectedItem);
            query = query.Where(s => s.Ent == SelectedValue);
        }

        //When filtering between these 2 dates, I get no results

        if (DataRFrom.Checked && DataRTo.Checked)
        {
            DateTime begin = DataRFrom.Value.Date;
            DateTime end = DataRTo.Value.Date;
            query = query.Where(s => s.DataR > begin && s.DataR < end);
        }
        return query.ToList();
    }

In the SQL, I have dates like this... Perhaps the Date format in SQL are messing up all this??? 在SQL中,我有这样的日期……也许SQL中的Date格式搞砸了所有这一切???

在此处输入图片说明

My questions is, why can't I get any results between 2018-06-01 and 2018-06-06 since records with this date range are in Database? 我的问题是,为什么在此日期范围内的记录在数据库中,为什么在2018-06-01和2018-06-06之间无法得到任何结果?

Thanks. 谢谢。

Date property is missing: 日期属性丢失:

query = query.Where(s => s.DataR.Date > begin && s.DataR.Date < end); query = query.Where(s => s.DataR.Date> begin && s.DataR.Date <end);

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

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