我怎么能直接执行在LINQ SQL查询 [英] How can I directly execute SQL queries in linq
问题描述
在C#与VS 2008,我有一个查询,在该查询我参加一个以上的表,所以我不知道是什么类型,我想知道如何直接运行在LINQ SQL查询。
In C# with VS 2008,I have a query ,In this query i join more than one tables,so i don't know the type , I want to know how to directly run a sql query in linq .
IEnumerable<Type> results = db.ExecuteQuery<TYpe>("sql query")
我上面的查询工作正常,但我要避免的类型,我想写
My above query works fine but I want to avoid type, I want to write
var results = db.ExecuteQuery("sql query");
有没有办法写呢?
Is there any way to write it?
先谢谢了。
推荐答案
您需要指定映射到从查询结果的类型。您可以使用的System.Type
对象而不是静态地将其指定为一个泛型类型参数:
You need to specify the type to map to from the query results. You can use a System.Type
object instead of statically specifying it as a generic type parameter:
var results = db.ExecuteQuery(typeof(Customer), "sql query ");
如果你只是想一个普通的ADO.NET DataReader的,你可以使用的DataContext ■连接
属性:使用
If you just want a plain ADO.NET DataReader you could use the DataContext.Connection
property:
using (var cmd = db.Connection.CreateCommand())
{
cmd.CommandText = "sql query ";
var results = cmd.ExecuteReader();
}
这篇关于我怎么能直接执行在LINQ SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!