Converting System.Linq.IQueryable interface to System.Data.DataSet

By | May 20, 2011

Just a quick tip, in case you need to convert a System.Linq.IQueryable interface to System.Data.DataSet then check at the codes below

private DataSet LINQToDataSet({Your LINQ Data Context} myDB, IQueryable item)
{
    SqlCommand cmd = myDB.GetCommand(item) as SqlCommand;

    DataTable oDataTable = new DataTable();
    SqlDataAdapter oDataAdapter = new SqlDataAdapter(cmd);
    oDataAdapter.Fill(oDataTable);

    DataSet oDataSet = new DataSet();
    oDataSet.Tables.Add(oDataTable);
    return oDataSet;
}

Usage:

var oTableResult = (from p in myDB.SampleTable
                where p.SampleColumn == "Test Value"
                select p);
LINQToDataSet(myDB, oTableResult);
Recommended

One thought on “Converting System.Linq.IQueryable interface to System.Data.DataSet

  1. bdgngcr

    When I use this method IDE doesn’t allow “myDB.GetCommand(item)” part of code.My dbContext in DataLayer.I want to reach to DbContext from another Layer.{Your LINQ Data Context} myDB I changed this code like this DATALAYER.MyContext myDB. But IDE underlines to GetCommand(item) part of code.
    How to solve this problem ?Any recommendation?

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.