Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to SQL column has keyword as column name, how to quote/escape

In a LINQ to SQL statement I've got a column name in the database that's also a C# keyword (void). How do I get the compiler to treat this as an object property and not the keyword?

I could re-write this in with the method notation, sure, but there's got to be a way to give the compiler a hint here...

var p = from c in mytable
        where c.id = rowNumber
        select ( new { voidedState = c.void } );  // <--- Problem is here

The error is:

Identifier expected; 'void' is a keyword

I can't argue with that, I'm just looking for a workaround.

Thanks.

like image 832
Clinton Pierce Avatar asked Apr 16 '09 17:04

Clinton Pierce


People also ask

How do you escape keywords in SQL?

To escape reserved keywords in DDL statements, enclose them in backticks (`). To escape reserved keywords in SQL SELECT statements and in queries on views, enclose them in double quotes ('').

How do you handle SQL column names that look like SQL keywords?

Both these are keywords in SQL. The trick here is to type the column names inside the square brackets '[]' so that they are not read as a Reserved Word by the compiler.

Do column names need quotes in SQL?

If the table names and the column names are shown in lowercase letters this means they are delimited and quotation marks need to be used for the table names and the column names when executing SQL queries against the Oracle database.


1 Answers

Precede the identifier with a "@":

@void

Is the name of an identifier "void", not the keyword (this works anywhere an identifier is needed).

like image 146
Richard Avatar answered Sep 17 '22 17:09

Richard