简体   繁体   中英

Is it possible to make LinQ “understand” a classic SQL query?

Is it possible to make LinQ to SQL read and understand a pure SQL query such as the following?

SELECT * from myTable

My boss asked me to write directly to database the pure SQL querys in a certain table and this table will have a field named "typeOfSelect", for example.

And, in my application, I will have to read this field "typeOfSelect" and make LinQ understands what that means.

Thanks!

You can use the ExecuteCommand and ExecuteQuery methods on the DataContext.

var db = new MyDataContext();
IEnumerable<Table1> result = db.ExecuteQuery<Table1>("SELECT * FROM Table1");
db.ExecuteCommand("UPDATE Tabel1 SET Column1 = 'Foo'");

ExecuteQuery will work with any object that has property names that match the column names of the table. ExecuteCommand is used for SQL commands that don't return a result set.

Another way would be to use the Connection.CreateCommand() method on a DataContext which will return a DbCommand object. Then you simply set the CommandText property with the SQL you want to execute.

DbCommand command = myDataContext.Connection.CreateCommand();
command.CommandText = "SELECT * FROM Table1";

try {
    command.Connection.Open();
    DbDataReader reader = command.ExecuteReader();
    // do something usefull with the reader like creating a DataTable
} finally {
    command.Connection.Close();
    command.Dispose();
}

You can also look at this MSDN link for some other examples.

If you want to write SQL it is easy to just create some wrapper classes so you want write something like this.

DataTable result = DBManager.Query("SELECT * FROM Table");

But I don't think this is generally considered good practice any more and would defeat the purpose of Linq.

LINQ is the means by which Microsoft differentiates their server access technology from more generic, portable techniques. By making it easier for programmers to do it the "Microsoft Way", they gain competitive advantage. Once you're convinced that LINQ is the universal querying syntax, with applicability beyond relational databases (which is the primary destination, and where the competition is very thin), they have a strong interest in maintaining full control of the language (especially since it will work best with the rest of their infrastructure).

This is where we'll find out how well the "Leaky Abstraction" concept plays out. Microsoft certainly has what I think are the best minds in the industry committed to it, and a lot of people are betting pretty heavily on it. But it's a 100% commitment - no looking back.

It's a lot like asking whether you can use SQL to express ISAM queries. And at one time it was IBM driving the SQL query infrastructure, in an equally semi-proprietary way, as I've heard it told.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM