Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

C# EF: date compare and no data is coming

$
0
0

i have a data in table and i am fetching data with date range and country code but no data is coming.

when i am fetching data by sql query like below then right set of data is coming but when fetching the same from EF query then no data is coming.

my sql query which is working fine.

Declare  @NPSStartDate   datetime
Declare  @NPSEndDate   datetime

SET @NPSStartDate = '01/01/2017'
SET @NPSEndDate =   '01/05/2017'

    SELECT * FROM NPSData WHERE CONVERT(varchar(10),NPSDate,112) >= CONVERT(varchar(10),@NPSStartDate,112)
        AND CONVERT(varchar(10),NPSDate,112) <= CONVERT(varchar(10),@NPSEndDate,112) AND CountryCode='GB'
        ORDER BY CONVERT(varchar(10),NPSDate,112),CountryCode

the same query when done by EF then no data is coming.

DateTime stdate=DateTime.Parse("01/01/2017");//format dd/MM/yyyyDateTime etdate =DateTime.Parse("05/01/2017");//format dd/MM/yyyy

    using (var db =newNPSDbContext()){var query =(from n in db.NPSDatasorderby n.AddDate,n.CountryCodewhere n.CountryCode=="GB"&&(n.AddDate>= stdate.Date&& n.AddDate<= etdate)select n).ToList();}

when i compare date with MM/dd/yyyy format then data comes but many data come which is not right.

my AddDate data type is nullable date time

public DateTime? AddDate { get; set; }

i am not being able to understand where i am making the mistake ? please guide me how to rectify it.

also tell me what is best practice for EF date range query when i am do not know what date time format is set for client sql server.

thanks


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>