tpow tpow - 1 month ago 19
C# Question

LinqToExcel syntax

I am using the LinqToExcel project developed by MIT and hosted on Google Code at http://code.google.com/p/linqtoexcel/wiki/UsingLinqToExcel.

It seems pretty straight forward and elegant. I was able to rewrite a method that used the MS excel interop library and the code was about 1/3 the size.

However, I ran into an issue with trying to query a range of cells. VS2008 picks it up as a syntax error:

//These lines are fine
IEnumerable<string> names = new List<string>();
var excel = new Excel.ExcelQueryFactory(_excelFilePath);

//This line shows a syntax error starting from c[0]
names = from c in excel.WorksheetRange("A1", "AI1", headerName)
c[0] == "IN"
select c;


The line - c[0] == "IN" - just seems strange. This should grab the value in cell A1. If I remove "c[0] ==IN" The syntax error goes away, but it returns no results.

Is this syntax correct? Is the code on the linked page C#?




UPDATE: After getting some answers, it seems the missing "Where" is indeed a typo. However, even with the "where" I couldn't get c[4] == "IN" to return cell A5. I was able to accomplish what I needed to by removing the entire where clause, which returned me the entire range specified. In the initial post, I was just trying to return a singe value - baby steps :)

For the sake of marking an answer - how would I return just one cell in the range? Perhaps the
== "IN"
is some sort of typo, and not an actual construct of LinqToExcel?

Thanks for your help!

Answer

It should have been:

var names = from c in excel.WorksheetRange("A1", "AI1", headerName) 
            where c[0] == "IN" 
            select c; 
Comments