tags:

views:

93

answers:

4
+1  Q: 

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!

+2  A: 

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

Pharabus
There should be a `var` in there too.
sshow
@sshow you are correct, answer updated, thanks
Pharabus
@Pharabus @sshow I don't think so, the variable `names` is already defined above. However it's better to remove the first declaration,isn't it?
Danny Chen
@Danny Chen you are correct, personally I would do as you suggest and get rid of the initial declaration
Pharabus
@Danny Chen - thanks, I think it is easier to assign directly to var as well.
cinqoTimo
+1  A: 

Try this:

   //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)
                where c[0] == "IN"
                select c;
Alex Reitbort
c[0] == "IN", a typo :=)
Danny Chen
@Alex Reitbort - this results in "Operator '==' can't be applied to LinqToExcel.Row and string" However, when I try to cast "c" to a string I get the same thing except "....Method group and string"
cinqoTimo
@cinqo: Danny said that I missed index in c. Fixed.
Alex Reitbort
+6  A: 

It should have been:

var names = from c in excel.WorksheetRange("A1", "AI1", headerName) 
            where c[0] == "IN" 
            select c; 
klausbyskov
@Klausbyskov - You are correct in that the "Where" was missing, but something else is wrong with this query as it return no results..
cinqoTimo
+2  A: 

On that page, about half the queries shown have the where and half don't. I think it's just a copy'n'paste error. That's not a part of the syntax that LinqToExcel would have any control over.

However, I do note that you said, without that line, no results are returned. This is another problem, since, without that line, every row should have been returned (although, if I'm reading it right, your range is only one row long). However, I'll assume that LinqToExcel works like LinqToSql and LinkToObject, and that it is lazy evaluated, meaning it doesn't do the query until you actual step through it.

And another point:

 IEnumerable<string> names = new List<string>(); 
 names = from c in excel.WorksheetRange("A1", "AI1", headerName) 
            where c[0] == "IN" 
            select c; 

The first line creates a list of strings, which is then thrown away at the next line. Many new C# programmers seem to think the new is need to declare the type of the variable, but that handled completely here by the IEnumerable<string> part. The new is unnecessary. That line could be written as:

 IEnumerable<string> names = null;

or just

 IEnumerable<string> names;

or it could be combined with the second line:

 IEnumerable<string> names 
     = from c in excel.WorksheetRange("A1", "AI1", headerName) 
            where c[0] == "IN" 
            select c; 

or shortened to:

 var names = from c in excel.WorksheetRange("A1", "AI1", headerName) 
            where c[0] == "IN" 
            select c; 
James Curran
good point +1 re lazy evaluation
Pharabus
@James Curran - It was your post that led me to remove the Where clause, which in turn, solved my problem. And thanks for the tip - you're correct, I'm new to C#!
cinqoTimo