title: How to using Linq with Dataview?
body: <p>I am learning linq to dataset from this post: <a href="https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/creating-a-dataview-object-linq-to-dataset" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/creating-a-dataview-object-linq-to-dataset</a>
  and I am having trouble when running this code, I do not understand why it doesn't work, however
  The code works fine if I delete this line: </p>
  
  <pre><code>Where order1.Field(Of Integer)("Column2") = 1
  </code></pre>
  
  <p>my table have 4 columns corresponds to the name: Column1,2,3,4
  Here is vbcode:</p>
  
  <pre><code>Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
       Try
          Dim MyConnection As System.Data.OleDb.OleDbConnection
          Dim DtSet As System.Data.DataSet = New System.Data.DataSet
          Dim MyCommand As System.Data.OleDb.OleDbDataAdapter
          MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source='C:\Users\Username\Downloads\Danh STT.xlsx';Extended Properties='Excel 12.0 Xml;HDR=YES;'")
          MyCommand = New System.Data.OleDb.OleDbDataAdapter _
              ("select * from [Sheet1$]", MyConnection)
          MyCommand.TableMappings.Add("Author", "TestTable")
          MyCommand.Fill(DtSet)
          Dim orders As DataTable = DtSet.Tables(0)
          Dim query = From order1 In orders.AsEnumerable
                      Order By order1.Field(Of String)("Column1")
                      Where order1.Field(Of Integer)("Column2") = 1
                      Select order1
          Dim view As System.Data.DataView = New Data.DataView
          view = query.AsDataView()
          DataGridView1.DataSource = view
          MyConnection.Close()
      Catch ex As Exception
          MsgBox(ex.ToString)
      End Try
  End Sub
  </code></pre>
  
  <p>Can you explain for me please!
  Thanks a lot.
  Edit: [The error I received]</p>
  
  <blockquote>
    <p>{System.InvalidCastException: Specified cast is not valid." &amp; vbCrLf &amp; "   at System.Data.DataRowExtensions.UnboxT<code>1.ValueField(Object value)" &amp; vbCrLf &amp; "   at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)" &amp; vbCrLf &amp; "   at Dataview.Form1._Closure$__._Lambda$__1-0(DataRow order1) in D:\2.0_VSTO_PROJECT\D_1_MY_PROJECT\1 EXCEL\.NET\1 Class .Net\LinqExample\Dataview\Dataview\Form1.vb:line 31" &amp; vbCrLf &amp; "   at System.Data.EnumerableRowCollection</code>1.b__16_0(DataRow row)" &amp; vbCrLf &amp; "   at System.Data.EnumerableRowCollection<code>1.&lt;&gt;c__DisplayClass16_0.&lt;GetLinqDataView&gt;b__3(DataRow row)" &amp; vbCrLf &amp; "   at System.Data.DataView.RowPredicateFilter.System.Data.IFilter.Invoke(DataRow row, DataRowVersion version)" &amp; vbCrLf &amp; "   at System.Data.Index.AcceptRecord(Int32 record, IFilter filter)" &amp; vbCrLf &amp; "   at System.Data.Index.InitRecords(IFilter filter)" &amp; vbCrLf &amp; "   at System.Data.Index..ctor(DataTable table, IndexField[] indexFields, Comparison</code>1 comparison, DataViewRowState recordStates, IFilter rowFilter)" &amp; vbCrLf &amp; "   at System.Data.DataView.UpdateIndex(Boolean force, Boolean fireEvent)" &amp; vbCrLf &amp; "   at System.Data.DataView.UpdateIndex(Boolean force)" &amp; vbCrLf &amp; "   at System.Data.DataView.SetIndex2(String newSort, DataViewRowState newRowStates, IFilter newRowFilter, Boolean fireEvent)" &amp; vbCrLf &amp; "   at System.Data.DataView..ctor(DataTable table, Predicate<code>1 predicate, Comparison</code>1 comparison, DataViewRowState RowState)" &amp; vbCrLf &amp; "   at System.Data.EnumerableRowCollection<code>1.GetLinqDataView()" &amp; vbCrLf &amp; "   at System.Data.DataTableExtensions.AsDataView[T](EnumerableRowCollection</code>1 source)" &amp; vbCrLf &amp; "   at Dataview.Form1.Button1_Click(Object sender, EventArgs e) in D:\2.0_VSTO_PROJECT\D_1_MY_PROJECT\1 EXCEL.NET\1 Class .Net\LinqExample\Dataview\Dataview\Form1.vb:line 36}</p>
  </blockquote>