Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hello,

I need the equivalent query in LINQ. Can any one please help me ?

SQL
Select count(*),Name 
from Test
where convert(varchar(12),postdate,101) =  convert(varchar(12),GETDATE(),101)
group by Name



Thank you.
Posted
Updated 9-May-11 10:30am
v2

This is my version of the solution.


 var tomorrow = DateTime.Now.Date.AddDays(1);
 var today = DateTime.Now.Date;
 
 var ts = from t in Tests
     where t.postdate >= today && t.postdate < tomorrow
     group t by t.Name into newt
     select new { Name = newt.Key, Count = newt.Count() };
	 
foreach ( var t in ts) {
	Console.WriteLine("{0} {1}",t.Count, t.Name);
}


It finds the Test entries that were posted today and counts after grouping by name.
 
Share this answer
 
SQL
DataClasses1DataContext dc = new DataClasses1DataContext();

   var query = from c in dc.Test
        where Convert.ToString(postdate) == Convert.ToString(DateTime.Now.Date)
        group c by c.Name into g
        select new  { Count = g.Count() , Name = g.Key  };


I just tested, this code works.
 
Share this answer
 
I think this would be the more or less equivalent:

from test in testcollection
where test.postdate == DateTime.Now.Date
group test by test.name into newtest
select new { Name = newtest.Name, PostDate = newtest.postdate };
 
Share this answer
 
Comments
Mastersev 9-May-11 17:18pm    
where is the count?
Mastersev 9-May-11 17:19pm    
from c in dc.Test
where Convert.ToString(postdate) == Convert.ToString(DateTime.Now.Date)
group c by c.Name into g
select new { Count = g.Count() , Name = g.Key }

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