Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I translate this GROUP BY / MIN SQL query into LINQ?

Tags:

I plan on using this in a subquery but can't figure out the correct syntax to translate the following query into LINQ:

select ChapterID, min(MeetingDate) from ChapterMeeting group by ChapterID 
like image 636
Even Mien Avatar asked Mar 17 '09 13:03

Even Mien


People also ask

How do you do GroupBy in LINQ?

Group by single property example The following example shows how to group source elements by using a single property of the element as the group key. In this case the key is a string , the student's last name. It is also possible to use a substring for the key; see the next example.

How do I run a SQL query in LINQ?

Add a LINQ to SQL class file. Drag and drop the respective table. Now, copy this code in the main method. We are creating an instance of sample datacontext class and then we are using this ExecuteQuery method to execute the SQL query.

How do you write a query in LINQ?

This query returns two groups based on the first letter of the word. List<int> numbers = new() { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; // The query variables can also be implicitly typed by using var // Query #1. IEnumerable<int> filteringQuery = from num in numbers where num < 3 || num > 7 select num; // Query #2.

How LINQ queries converted into SQL queries?

LINQ to SQL translates the queries you write into equivalent SQL queries and sends them to the server for processing. More specifically, your application uses the LINQ to SQL API to request query execution. The LINQ to SQL provider then transforms the query into SQL text and delegates execution to the ADO provider.


1 Answers

var query = myDataContext.ChapterMeeting   .GroupBy(cm => cm.ChapterID)   .Select(g => new {       g.Key,       MinMeetingDate = g.Min(cm => cm.MeetingDate)   }); 
like image 150
Amy B Avatar answered Oct 25 '22 21:10

Amy B