0

I am trying to create a LINQ query that will filter out data with criteria from two different columns.

I am trying to get data between two dates from Time_Data_1 column only when a specific string value is met from Section_Data column, ie, Between 2016/5/15 and 2016/6/16 when SelectedOption = Something This is what I have, which is not working.

 DataGridOne.DataContext = sql.Time_TBLs.Where(item =>
                item.Time_Data_1 < new DateTime(DateTime.Now.Year, DateTime.Now.Month - 1, 15) &&
                item.Time_Data_1 > new DateTime(DateTime.Now.Year, DateTime.Now.Month, 16) &&
                item.Section_Data == SelectedOption);

This query below works for the single criteria of getting data between two dates. But it picks up every bit of data between those dates.

Column1.DataContext = sql.Time_TBLs.Where(item =>
                    item.Time_Data_1 < new DateTime(DateTime.Now.Year, DateTime.Now.Month - 1, 15) &&
                    item.Time_Data_1 > new DateTime(DateTime.Now.Year, DateTime.Now.Month, 16)

How do add a second criteria to the query?

EDIT: Typo, supposed to && when I put &

KyloRen
  • 2,691
  • 5
  • 29
  • 59
  • 1
    *"This is what I have, which is not working"*. 'Not working' is never enough description of a problem. In what way the query is not working? What did it return that you don't want? – har07 Jun 04 '16 at 05:23
  • @har07, that is it, it returns nothing. ie, not working – KyloRen Jun 04 '16 at 05:26
  • 1
    You want to filter between two dates i.e between `2016/05/15` and `2016/06/16` but your condition says you want less then `2016/05/15` and greater than `2016/06/16`. `DataGridOne.DataContext = sql.Time_TBLs.Where(item => item.Time_Data_1 > new DateTime(DateTime.Now.Year, DateTime.Now.Month - 1, 15) && item.Time_Data_1 < new DateTime(DateTime.Now.Year, DateTime.Now.Month, 16) && item.Section_Data == SelectedOption);` Try this It will work. – Fanjo Lama Jun 04 '16 at 05:29
  • @FanjoLama. Thanks, that sorted me out. Sometimes the simplest things can allude you. I was concentrating on syntax so much I just did not see that. Thanks – KyloRen Jun 04 '16 at 05:36
  • @KyloRen your welcome. I will post this. Will you mark this answer. – Fanjo Lama Jun 04 '16 at 05:37
  • @FanjoLama, Done. Thanks again. – KyloRen Jun 04 '16 at 05:38
  • @KyloRen Thanks. Happy Coding. – Fanjo Lama Jun 04 '16 at 05:39

2 Answers2

1

Any specific error/issue you are getting?

As far as code goes all I would say is possibly BODMAS and bitwise & operator. Read more on & on Usage & versus &&

Meanwhile does this code work for you?

DataGridOne.DataContext = sql.Time_TBLs.Where(item =>
(item.Time_Data_1 < new DateTime(DateTime.Now.Year, DateTime.Now.Month - 1, 15) &&
item.Time_Data_1 > new DateTime(DateTime.Now.Year, DateTime.Now.Month, 16)) &&
item.Section_Data == SelectedOption);

PS : When working with pure DateTime, i find it easier to do 17 instead of 16 for dates. As all times default to 00:00:0000, and hence if you have data like 16/06/2016 07:05:0000 it would not be fetched back from the query that checks for DateTime.Day of 16.

Community
  • 1
  • 1
touchofevil
  • 595
  • 4
  • 21
1

Conditional statement is wrong. You want to filter between two dates i.e between 2016/05/15 and 2016/06/16 but your condition says you want less then 2016/05/15 and greater than 2016/06/16.

Your code should be like this.

DataGridOne.DataContext = sql.Time_TBLs.Where(item =>
item.Time_Data_1 > new DateTime(DateTime.Now.Year, DateTime.Now.Month - 1, 15)
&& item.Time_Data_1 < new DateTime(DateTime.Now.Year, DateTime.Now.Month, 16) 
&& item.Section_Data == SelectedOption);
Fanjo Lama
  • 581
  • 3
  • 15