none
Doubts using Linq RRS feed

  • Question

  • Hi All,

     

    I am initiating with LINQ and I have a basic doubt. Is there a way to return the result of a Linq select as a Dataset class ?

     

    I have a method like that:

     

    public void SelectAll()

    {

    var query = from u in db.TB_USERs

    select new {Code = u.CD_USER,

    Name = u.NM_USER,

    Birhtday = u.DT_BIRTH_USER,

    Document = u.NU_DOC_USER,

    Telephone = u.NU_TEL_USER};

    }

     

    I´d like to set a dataset with query variable result and return it in the method, like:

     

    public DataSet SelectAll()

    {

    var query = from u in db.TB_USERs

    select new {Code = u.CD_USER,

    Name = u.NM_USER,

    Birhtday = u.DT_BIRTH_USER,

    Document = u.NU_DOC_USER,

    Telephone = u.NU_TEL_USER};

     

    DataSet ds = new DataSet();

     

    ... do something here to transform data into a dataset !!!

     

    return ds;

    }

     

    Is is possible ? Is not, is there any way to get the Linq result and return it as some kind of list ? My need this, cause I am thinking about building a Linq Class, which any will instantiate and so, use its CRUD methods.

     

    Another doubt is, how can I use a concatenation form to execute a Linq command ? Like :

     

    public void SelectAll(string orderByField)

    {

    var query = from u in db.TB_USERs

    orderby + orderByField concatenated here

    select new {Code = u.CD_USER,

    Name = u.NM_USER,

    Birhtday = u.DT_BIRTH_USER,

    Document = u.NU_DOC_USER,

    Telephone = u.NU_TEL_USER};

    }

     

    And I wish to use this resource with other commands, like group by, number of fields to include in selection etc.

     

    Thanks.

    Thursday, November 6, 2008 2:34 PM

Answers

  • Add this to your project.  

     

    public static class DataContextExtensions

    {

        public static DataSet ExecuteAsDataSet(this DataContext dc, string tableName, IQueryable query)

        {

            var cmd = dc.GetCommand(query);

            var reader = cmd.ExecuteReader();

            DataSet ds = new DataSet();

            ds.Load(reader, LoadOption.OverwriteChanges, tableName);

            return ds;

        }

    }

     

    You can ask the DataContext for the DbCommand it would have used to execute the query.  Then you can simply execute the command object and use its DataReader to fill a DataSet.
    Friday, November 7, 2008 4:14 PM
    Moderator

All replies

  • Hi Oliver,

                   Here is an example of linq with dataset

                  

    public void DataSetLinq4() {

    var customers = testDS.Tables["Customers"].AsEnumerable();

    var waCustomers =

    from c in customers

    where c.Field<string>("Region") == "WA"

    select c;

    Console.WriteLine("Customers from Washington and their orders:");

    foreach (var customer in waCustomers) {

    Console.WriteLine("Customer {0}: {1}", customer.Field<string>("CustomerID"), customer["CompanyName"]);

    foreach (var order in customer.GetChildRows("CustomersOrders")) {

    Console.WriteLine(" Order {0}: {1}", order["OrderID"], order["OrderDate"]);

    }

    }

     

    I hope this one will  help you...

     

     

    Venky

    Friday, November 7, 2008 7:19 AM
  • Add this to your project.  

     

    public static class DataContextExtensions

    {

        public static DataSet ExecuteAsDataSet(this DataContext dc, string tableName, IQueryable query)

        {

            var cmd = dc.GetCommand(query);

            var reader = cmd.ExecuteReader();

            DataSet ds = new DataSet();

            ds.Load(reader, LoadOption.OverwriteChanges, tableName);

            return ds;

        }

    }

     

    You can ask the DataContext for the DbCommand it would have used to execute the query.  Then you can simply execute the command object and use its DataReader to fill a DataSet.
    Friday, November 7, 2008 4:14 PM
    Moderator