How can I convert the query from SQL to LINQ?

How to convert the SQL query into LINQ?

  • I am new to LINQ. Can anyone please help me convert the following SQL query to LINQ? SELECT Date,ShiftName,Max(Score) AS Score, 1 AS IsPreferred FROM Temp_Nurse_Fill_RequestNumbers group by ShiftName,date

  • Answer:

    from t in Temp_Nurse_Fill_RequestNumbers group by t.ShiftName, t.date into g select new { Date = g.date, Score = (from tt in g select tt.Score).max IsPreferred = 1 };

Navaneethakrishnan at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

Hmm, how about something like this (this is C#, I presume it is trivial to convert) temps .GroupBy(x => new {x.ShiftName, x.Date}) .Select(g => new TempNurseProjection { Date = g.Key.Date, ShiftName = g.Key.ShiftName, MaxScore = g.Max(q => q.Score), IsPreferred = true });

Ian Nelson

Just for convenience tems. GroupBy(Function(x) New With { x.ShiftName, x.Date }). Select(Function(g) New TempNurseProjection With { .Date = g.Key.Date, .ShiftName = g.Key.ShiftName, .MaxScore = g.Max(Function(q) q.Score), .IsPreferred = True }) EDIT Sorry didn't noticed that the final projection is not anonymous but named type.

Oybek

You can use a tool called LINQPad. Tired of querying in antiquated SQL? Well, you don't have to! LINQPad lets you interactively query databases in a modern query language: LINQ. Kiss goodbye to SQL Management Studio! ... LINQPad is also a great way to learn LINQ: it comes loaded with 500 examples from the book, C# 4.0 in a Nutshell. There's no better way to experience the coolness of LINQ and functional programming. http://www.linqpad.net/ hope this helps

dknaack

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.