将查询结果绑定到datatable

时间:2010-08-22 10:47:21

标签: c# vb.net datatable

欢迎使用C#或VB.NET中的示例。

我想将以下查询结果绑定到datatable:

Dim query = From c in db.Customers _
            Where c.Status = "Active" _
            Select c.CId, c.FirstName, c.LastName, c.Email

感谢。

1 个答案:

答案 0 :(得分:1)

试试这个:

Dim dtCustomer = New DataTable("Customer")
        dtCustomer.Columns.Add("CId", GetType(Integer))
        dtCustomer.Columns.Add("FirstName", GetType(String))
        dtCustomer.Columns.Add("LastName", GetType(String))
        dtCustomer.Columns.Add("Email", GetType(String))

        For Each q In query
            dtCustomer.Rows.Add(New Object() {q.CId, q.FirstName, q.LastName, q.Email})
        Next

查看DataTable class on MSDN