Date filtering issues in Linq Query
-
Hi, Im facing the probelm in filtering the record using the date criteria. Im having 2 column FromDate and ToDate. Eg: FromDate : 28/04/2011 & ToDate : 11/03/2012 If i pass the FromDate as 04/09/2011 and ToDate as 10/09/2011 means i need to get the above the record, but i cant able to the data. Query i used date criteria: ------------------------------ [FromDate] >= FromDate.Value && [FromDate] <= ToDate.Value && [ToDate] >= FromDate.Value && [ToDate] <= ToDate.Value Can i know the pblm in the above query. Thanks, Senthil V.
-
Hi, Im facing the probelm in filtering the record using the date criteria. Im having 2 column FromDate and ToDate. Eg: FromDate : 28/04/2011 & ToDate : 11/03/2012 If i pass the FromDate as 04/09/2011 and ToDate as 10/09/2011 means i need to get the above the record, but i cant able to the data. Query i used date criteria: ------------------------------ [FromDate] >= FromDate.Value && [FromDate] <= ToDate.Value && [ToDate] >= FromDate.Value && [ToDate] <= ToDate.Value Can i know the pblm in the above query. Thanks, Senthil V.
Firstly, looking at those square brackets on table column names, it appears to me this T-SQL not Linq as you say it is? If it is T-SQL here goes it:
"SELECT \* FROM \[Table\] WHERE \[FromDate\] >= '" + FromDate.Value + "' AND \[ToDate\] <= '" + ToDate.Value + "';"
Otherwise if it's indeed Linq-SQL then try this:
var myTableData = dataContextInstance.MyTable.Where( e => e.FromDate >= DateTime.Parse( FromDate.Value ) && e.ToDate <= DateTime.Parse( ToDate.Value ) ).ToList();
//This will simply return a list or data matching your linq-query and there are many ways you can loop through a list e.g. for-loop, each loop
hope this will help otherwise clarify a little on ya question. Morgs
-
Hi, Im facing the probelm in filtering the record using the date criteria. Im having 2 column FromDate and ToDate. Eg: FromDate : 28/04/2011 & ToDate : 11/03/2012 If i pass the FromDate as 04/09/2011 and ToDate as 10/09/2011 means i need to get the above the record, but i cant able to the data. Query i used date criteria: ------------------------------ [FromDate] >= FromDate.Value && [FromDate] <= ToDate.Value && [ToDate] >= FromDate.Value && [ToDate] <= ToDate.Value Can i know the pblm in the above query. Thanks, Senthil V.
Hi, Try to reverse the comparissons and see what happens ;)