CheYuan Lin
CheYuan Lin

Reputation: 1

LinqToExcel:How to get specific row in Excel

I have an excel that looks like

ID   Name   DepartmentID Ext
1298 Alice  AA1152       1221
1299 Andrew AA1152       1235
2894 Jack   AA1152       1285
2723 Daniel AA1152       4239

All employees are in the same department, and I want to get the DepartmentID from excel to link to the department name in database.

I've read this tutorial http://www.codeproject.com/Articles/659643/Csharp-Query-Excel-and-CSV-Files-Using-LinqToExcel but it has to read the entire file and access sequentially in foreach loop.

How can I get only one row from excel using LinqToExcel library?

Thanks a lot.

Upvotes: 0

Views: 1248

Answers (2)

har07
har07

Reputation: 89305

You should be able to use First() to return only the first result :

var artistAlbums = from a in excelFile.Worksheet(sheetName) select a;
var firstAlbum = artistAlbums.First();

And to get one specific row, combine Skip() and First(), for example :

var fifthAlbum = artistAlbums.Skip(4).First();

Upvotes: 2

cableload
cableload

Reputation: 4385

Try something like this

var excelData = new ExcelQueryFactory(excelFileName);
var yourRow = from c in excelData.Worksheet<YourMappedClass>(WorksheetName) where c.Id==SomeValue select c;

There are quite a few other things you can accomplish as well as explained in their github page.

Upvotes: 0

Related Questions