In a short: You have to use
ADO.NET[
^] (OleDb) provider to connect to the Excel sheet, then you'll be able to use
Linq to DataSet[
^].
Steps to do:
1. Create
OleDbConnection[
^] to connect to the Excel file (
connectionstring[
^])
Dim sConnStr As String = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR={1}';", "FullFileName.xlsx", "YES|NO")
Dim oConn As OleDbConnection = New OleDbConnection(sConnStr)
2. Create
OleDbCommand[
^]
Dim sCommStr As String = "SELECT * FROM [Sheet1$];"
Dim oComm As OleDbCommand = New OleDbCommand(sCommStr, oConn)
3. Create
OleDbReader[
^] to read data into
DataTable[
^] (use
DataTable.Load(IDataReader) method[
^])
Dim oRdr As OleDbReader
Dim oDt As DataTable = New DataTable()
oRdr = oComm.ExecuteReader()
oDt.Load(oRdr)
4. Use Linq on datatable object
Dim myData = oDt.AsEnumerable() _
.Where(Function(x) x.Field(Of String)("Name") = "Peter") _
.Select(Function(x) x) _
.ToList()
More:
Queries in LINQ to DataSet | Microsoft Docs[
^]
Other resources:
Accessing Microsoft Office Data from .NET Applications[
^]
Using LINQ to Query Tables in Excel 2007 (OpenXML)[
^]
Walkthrough: Office Programming (C# and Visual Basic) | Microsoft Docs[
^] (there's small combobox on the right site to change language)
Create a simple data application by using ADO.NET[
^]