Search code examples
c#jquerylinqobjectlinq-to-excel

Linq / Linqtoexcel issue selecting


I have a weird issue which I totally not understand. Maybe I'm missing something.

var excel = new ExcelQueryFactory("products.xlsx");
//get all products with discount
var discounts = from s in excel.Worksheet() where
    s["Discount/Premium"].Cast<string>().StartsWith("-") select s;
//Select products with a YTM higher than 3
var applicable = from b in discounts where 
    b["YTM"].Cast<double>() > 3.0 select b;
MessageBox.Show(discounts.Count().ToString()); //<- returns 51
MessageBox.Show(applicable.Count().ToString()); //<- returns 14826
  • products.xlsx got around 50'000 Rows/Products
  • There are 51 products with discounts
  • There are 14'000 products with "YTM > 3.0" - HOW can this be?

To my my knowledge, I'm searching YTM products in discounts. How can I get 14'000 products from 51?!? Am I missing something?

Best regards, Julian


Solution

  • You are using a 3rd party library that allows you to do queries over the xls (Linq to Excel). But the issue in your second query should be due how was implemented the Linq provider to query the excel document. At the time you call ToList(), you are bringing those rows to memory, so you are no longer using that Linq provider, you are now using Linq to Object over the 51 rows.

    Usually when you compose queries as your case, the condition you are applying in your second query is treated as an && by the Linq provider. I don't know why is not the case in Linq to Excel