views:

136

answers:

1

Hello!

What is the SQL language keyword for the LINQ-to-SQL FirstOrDefault or SingleOrDefault?

Is it TOP(1)?

EXAMPLE:

SELECT TOP(1) @ItemCode = ItemCode FROM VendorItem WHERE VendorId = @VendorId

There can't be more than 1 results anyway since there is a Unique Key constranint, do I have to spell out the TOP(1) or whatever it is?

Note: I don't need LINQ answers, my question is how to write the sql script.

+2  A: 

If there is a unique key constraint you do not need to add anything to have the FirstOrDefault behavior. For other queries you can add

LIMIT 1

to the end of your SQL query. This will just give you the first answer which matches your constraints.

Edit after comment: To get it as a scalar in .NET you can use the SQLCommand.ExecuteScalar method.

Matthijs P
You mean that I can declare a scalar variable and select the results right to it. Correct me if I am wrong.
Shimmy
I actually needed its ret value for a SQL Computed Column, but anyway, from your update I could understand that I wasn't wrong :) thanks a lot.
Shimmy