1

I have come across the LinqToExcel library and have been playing around with it a bit. I seem to be mis-understanding how the querying works. I have created a simple worksheet with some customer info. Using LinqPad, I've setup my query as so:

void Main()
{
    string path = @"E:\Documents\LINQPad\LINQPad Queries\LinqToExcel\Customers.xlsx";

    var excel = new ExcelQueryFactory(path);

    // Mappings and Transforms:
    excel.AddMapping<Customer>(c => c.Contact, "Contact Person");
    excel.AddMapping<Customer>(c => c.CompanyName, "Company Name");
    excel.AddMapping<Customer>(c => c.IsPreferred, "Preferred");
    excel.AddTransformation<Customer>(c => c.IsPreferred, cellValue => cellValue == "Yes");

    // Query:
    var preferrdCompanies = from c in excel.Worksheet<Customer>("Customers")
                            where c.IsPreferred  // this has no effect?
                            orderby c.CompanyName
                            select new { c.CompanyName, c.Contact, c.IsPreferred };

    // Display:
    preferrdCompanies.Dump("Preferred Customers:");
}

// Define other methods and classes here
class Customer
{
    public string Contact { get; set; }
    public string CompanyName { get; set; }
    public bool IsPreferred { get; set; }   
}

For some reason, the predicate is not being applied. The transform to true(bool) from the text "Yes" is working. If I write:

preferrdCompanies.ToList().Where(c => c.IsPreferred).Dump("Preferred Customers:");

I get the filtered list as you would expect. I've been looking for a simple error in my code but it throws no exceptions and I can't find anything obviously wrong, so I guess I just don't understand how the querying functions?

Any answer/explanation would be appreciated, thanks.

Sleette
  • 366
  • 3
  • 13
  • If adding `ToList` before `Where` works then it's likely a bug in LinqToExcel. – D Stanley Dec 09 '14 at 22:38
  • That could be; I added ToList() to change the result set from an ExcelQueryable<> to a List<> just to get the filtered results. It acts as if the ExcelQueryable has a method to accept a predicate but does nothing with it. – Sleette Dec 09 '14 at 23:40
  • Possible; or there's a bug in the parsing that doesn't process a boolean column properly. – D Stanley Dec 09 '14 at 23:41

1 Answers1

0

Can you try explicitly setting c.IsPreferred == true in the where clause and see if that resolves your issue.

var preferrdCompanies = from c in excel.Worksheet<Customer>("Customers")
                        where c.IsPreferred == true
                        orderby c.CompanyName
                        select new { c.CompanyName, c.Contact, c.IsPreferred };
Paul
  • 18,349
  • 7
  • 49
  • 56
  • Oddly, if I add c.IsPreferred == true (or false), it simply returns a collection with no items. – Sleette Dec 09 '14 at 23:31