-1

I am new to LINQ and don't know how to write group by query. I have one table with three columns contact_id, order_id, product_id and I have two sql queries

Select Top 10 Count(order_id) 'No. Of Orders', order_id, contact_id from SampleData
Group by order_id, contact_id
Order by 1 Desc

--===========================================================================
--Most Popular products
--===========================================================================
Select Count(order_id) 'Most Popular products', product_id from SampleData
Group by product_id
Order by 1 Desc

How to write LINQ query for the same. Thanks in Advance.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 6
    Have you tried anything yourself? Do you have any sample code that didn't work? We'll help if you have specific issues but we're not here to write your code for you. – Justin Niessner Feb 17 '14 at 17:37
  • Check out this link: http://msdn.microsoft.com/en-us/library/system.linq.enumerable.groupby(v=vs.110).aspx It really depends upon how you have your data stored. Is it in a datatable? List? Something else? Need some more info to answer more completely – Pseudonym Feb 17 '14 at 17:42
  • provide your data model for us and describe, is it a collection or DB table values? Be more concrete please – melvas Feb 17 '14 at 17:42

1 Answers1

0

Take a look to this liker using that tool you can compare your sql statements and translate them into linq, that's a nice aid while you're learning linq, it's not going to help you only with groups by but to understand in a practical way how to use linq to sql.

this answer is a good reference to solve your query

Community
  • 1
  • 1
pedrommuller
  • 15,741
  • 10
  • 76
  • 126