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
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:
- How to convert below SQL Expression into derived column in SSIS?Best solution by Stack Overflow
- How to convert sql query to Hibernate Criteria query?Best solution by Stack Overflow
- How to convert my SQL query to MS Access query?Best solution by Stack Overflow
- How to convert a SQL query into hibernate criteria?Best solution by Stack Overflow
- How to convert SQL query to LINQ query?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.