Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a simple SQL Query. I am learning LINQ. can anyone help to understand, how can i convert SQl query to LINQ


Select LDRSectionReference from LDR.LDRFlagName Where SectionID=
(Select TOP 1 SectionID from LDR.LDRFlagName Order by 1 desc)


this above query is giving me the reference number from the table where sectionID is max.

What I have tried:

I have no idea to try.
Its a simple SQL query which return the reference detail from the table where sectionID is maximum.
Posted
Updated 26-Feb-20 20:29pm
v2

1 solution

Try this:
C#
var result = LDR.LDRFlagName
    .OrderByDescending(x=>x.SectionID)
    .Select(x->x.LDRSectionReference);


You can also use First()[^] or FirstOrDefault()[^] methods.
 
Share this answer
 
v3
Comments
Member 9956700 27-Feb-20 2:45am    
you meant..

var result = from LDRSectionReference in LDR.LDRFlagName
.OrderByDescending(x=>x.SectionID)
.Select(x->x.LDRSectionReference);
Maciej Los 27-Feb-20 2:53am    
If you say so... You're the owner of data ;)
Richard Deeming 27-Feb-20 6:41am    
That won't compile: CS0742 A query body must end with a select clause or a group clause

Remember that:
var result = from x in someQuery select x;
is identical to:
var result = someQuery;
Member 9956700 27-Feb-20 10:34am    
how to resolve this error?
Example

var a = from ss in studentList
.OrderByDescending(x=>x.StudentID).Select(x=>x.StudentName)
select ss.FirstOrDefault();

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