Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinqToExcel syntax

Tags:

c#

linq

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!

like image 757
tpow Avatar asked Jul 16 '26 00:07

tpow


2 Answers

It should have been:

var names = from c in excel.WorksheetRange("A1", "AI1", headerName) 
            where c[0] == "IN" 
            select c; 
like image 113
Klaus Byskov Pedersen Avatar answered Jul 17 '26 14:07

Klaus Byskov Pedersen


I think you need a where in there, it may be a typo on the doc page, have you tried

  //These lines are fine
   //IEnumerable<string> names = new List<string>(); removed this as not really needed
   var excel = new Excel.ExcelQueryFactory(_excelFilePath);

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

edit updated to add the var also needed

like image 20
Pharabus Avatar answered Jul 17 '26 14:07

Pharabus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!