Click here to Skip to main content
16,012,107 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to convert this query to linq

select year(CreatedAt),count(MemberID) from Members
where ID = 1
group by year(CreatedAt)
Posted

This is really not that hard. Struggle a little bit.
I suggest you download LinqPad[^], which you can use to test your queries online, and to play around with linq to anything. And by the way it contains a set of samples and tutorials - among which you will find several that will help you in solving this task.
 
Share this answer
 
Hi,
my advice is if you frequently using linq queries then,
use the below link and install sql to link converter,
even i use the same..
http://www.linqpad.net/[^]

it is so easy and you can just build any complex query in sql and then copy paste in linqpad then in a button click you will get a linq query.
even without having sound knowledge on Linq, i have completed a whole project which uses Linq :)


Good luck..
 
Share this answer
 
v2
Comments
Zoltán Zörgő 5-Jul-13 3:06am    
Good advice :) But just a little bit too late :D
Hi dineshdena,

Don't forget
C#
using System.Linq;

otherwise you can't use neither LINQ extension method and you need to create the class to store the data.
Here is the Code:
C#
[DataContract]
   public class MemberModel
   {
       [DataMember]
       public string year{ get; set; }
       [DataMember]
       public string Count{ get; set; }
   }

C#
var dataQuery =
    from Mem in Members
    where Mem.ID == 1
    group Mem by new { Mem.year(CreatedAt) } into g
    select new MemberModel()
    {
        year = g.Key.year(CreatedAt), 
        Count= g.Key.count(MemberID)
    };
 
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