Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a DateTime column in my table.

In Linq to SQL query, I only need Month part of that field

how to write the Linq query for that?

Thanks
Posted

Hi,

Write simple select query and use DateTime structure .ToString("MMMM") method to get month name.
I don't know your table and column names so I'll write some query and you will need to adjust your table and column names...
Since you didn't mentioned language i'll write simple code snippet in C#...
C#
YourDataContext dc = new YourDataContext();
var monthNames = from d in 			
			(
				from row in dc.GetTable<YourTable>()
				select row.YourDateTimeColumn
			).ToList()
			select d.ToString("MMMM");
					 
monthNames.Dump();

This will return month names for every record in your table.
If you want to select specific record use where in your query.
 
Share this answer
 
v2
Comments
Preethi PS 3-Sep-12 10:35am    
When i follow this in LINQPAD it is throwing NotSupportedException.How can i achieve this in LINQPAD?
Martin Arapovic 3-Sep-12 19:05pm    
Hi, I think that your problem/exception is related to datetime string format! Basically, LINQToSQL provider is getting tripped with it while trying to translate it into SQL when linq command is going to be executed. Above example is written without test and as I can see the easiest solution is to fetch datetime values into list and then do formatting in a second projection... I'll update example with code that should do the trick... :)
hiiii,

XML
object[] dt = { "13/01/2012","02/03/2012","04/03/2012","03/04/2012","06/05/2012" };
               List<string> result = (from d in dt select Convert.ToDateTime(d).ToString("MMMM")).ToList();
 
Share this answer
 

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