Pratik.S
Pratik.S

Reputation: 470

How to convert and assign result of a linq query to a datatable?

This is my code

    var s = from p in db.tblTotalFees
                where p.Class == ClassDropDownList.SelectedItem.Value && p.StudentID == Convert.ToInt32(StudentNameDropDownList.SelectedValue)
                select p;
        DataTable dt = new DataTable();

How can i convert p into a datatable and store it in dt???

Upvotes: 1

Views: 606

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236308

You can use CopyToDataTable() extension method which is described on MSDN How to: Implement CopyToDataTable Where the Generic Type T Is Not a DataRow. Usage:

var selectedClass = ClassDropDownList.SelectedItem.Value;
int id = Convert.ToInt32(StudentNameDropDownList.SelectedValue);

var query = from p in db.tblTotalFees
            where p.Class ==  && selectedClass
                  p.StudentID == id
            select p;

DataTable dt = query.CopyToDataTable(); // here

Or you can use pure ADO.NET - create SqlCommand and fill table with SqlDataAdapter.

Upvotes: 1

Related Questions