开发者

How can I directly execute SQL queries in linq

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 wr开发者_StackOverflowite

var results = db.ExecuteQuery("sql query");

Is there any way to write it?

Thanks in advance.


    var result = dataContext.ExecuteQuery<JobsDto>
                 ("Select JobID,JobName From jobs");

but make sure JobsDto has two properties JobID and JobName and there types the same type as the table columns

PS. DTO stand for Data Transfer Object


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 ");

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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜