Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table "DOB",
Which have two columns "Name" and "dateOfBirth",
now i want calculate total rows(result) which have
1. jan month,2012 year,
2. feb month,2012 year,
etc,

so how i use where condition, in the same column name "dateOfBirh" ,
which is in date time formet,
like 12/7/2012 12:24:00:PM,



Plz provide solution In Linq,
because i found solution in Sql like this,


SQL
select * from TableName

where datepart(mm, DateColumnName) = 1

and datepart(yyyy, DateColumnName) = 2012
Posted
Updated 28-Dec-12 22:55pm
v5

solution is:
C#
var data1 = context.t_quoted_value.Where(x => x.region_name == "Hong Kong"
                            && x.price_date.Value.Year == dt.Year
                            && x.price_date.Value.Month == dt.Month
                            && x.price_date.Value.Day == dt.Day).ToList();
 
Share this answer
 
I am assuming that you want to retrieve all the records where date of birth is on Jan 2012 or Feb 2012. If that's the case you can use DATEPART[^] to filter your rows.
try
SQL
SELECT COUNT(*) AS TotalRows FROM DOB
WHERE (DATEPART(Month,dateOfBirth) = 1 OR DATEPART(Month,dateOfBirth) = 2) AND (DATEPART(year,dateOfBirth) = 2012)


Edit:
SqlFunctions Class[^] might help you. Take a look at SqlFunctions.DatePart Method[^].
Also check out this CP article LINQ query to compare only date part of DateTime[^]
 
Share this answer
 
v3
Comments
Arun kumar Gauttam 29-Dec-12 3:13am    
yes your solution is right,i want this query in linq,
and linq not support DATEPART function,
__TR__ 29-Dec-12 4:30am    
SqlFunctions Class[^] might help you. Take a look at SqlFunctions.DatePart Method[^].
Also check out this CP article LINQ query to compare only date part of DateTime[^]
Arun kumar Gauttam 29-Dec-12 4:53am    
thanks,your last solution is work fine,
thanks for it, i am trying for this solution at so many days,
but today i found the solution,with the help of you
__TR__ 29-Dec-12 5:00am    
You are welcome. Glad it worked out for you. I have updated my answer to include those links to MSDN and the CP article so that it will be helpful for anyone referring this thread in the future.
Wendelius 2-Jan-13 3:36am    
Good answer, 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900