codeice / linqtoexcel

Automatically exported from code.google.com/p/linqtoexcel
0 stars 0 forks source link

where clause on NoHeader queries throws exception. #41

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

I have the simplest of boolean functions used in a "where" clause:

    static bool testWhere (Cell input) {
        return true;
    }

If I create a query such as this:
    from c in excel.WorksheetNoHeader("Sheet1") where (testWhere(c[0]) == true) select c

an exception is thrown:

    "(System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Data.DataException: '-1.get_Item(0' is not a valid column name. Valid column names are: ..."

What is the expected output? What do you see instead?

It seems for some reason the way I'm using the where clause is confusing the 
NoHeader version of the function.  I would expect the value of the cell to be 
passed to testWhere without throwing an exception.

What version of the product are you using? On what operating system?

I'm using 1.5.2 on XP x64 (using x86 as the target CPU).  Visual Studio 2010, 
.NET full framework.

Original issue reported on code.google.com by rotsiser...@gmail.com on 13 May 2011 at 10:10

GoogleCodeExporter commented 8 years ago
My apologies.  The above code will not reproduce the error, but this will:

XElement element = new XElement("Values", from c in 
excel.WorksheetNoHeader("Sheet1") where (testWhere(c[0]) == true) select new 
XElement("Value"));

Original comment by rotsiser...@gmail.com on 13 May 2011 at 10:14

GoogleCodeExporter commented 8 years ago
You can't use a cell value as an argument for a method in the where clause. 
It's not possible to create the sql statement from it. 

Here's a simple fix to your problem though. First download all the rows by 
casting it to a list and then apply your where clause to the in-memory list.

Here is how it would look in your code example:
XElement element = new XElement("Values", from c in 
excel.WorksheetNoHeader("Sheet1").ToList() where (testWhere(c[0]) == true) 
select new XElement("Value"));

Notice I added the 'ToList()' method.

Let me know if that doesn't resolve your issue.

Original comment by paulyo...@gmail.com on 23 May 2011 at 3:30