LINQ query not correctly translated when delegate variable used for predicate
Closed this issue · 2 comments
If I use this code:
repository.Invoices.Where(i => i.Status == "PAID");
it results in this GET request:
/api.xro/2.0/Invoices?WHERE='Status == "PAID"'
BUT if I use this code:
Func<Invoice, bool> isPaidInvoice = (i => i.Status == "PAID");
repository.Invoices.Where(isPaidInvoice);
it results in this GET request:
/api.xro/2.0/Invoices
which returns all invoices (not good).
I'm not totally sure that this should work, but it certainly isn't the behaviour that I would expect.
OK I just realised that it is my error and I can make it work using:
Expression<Func<Invoice, bool>> isPaidInvoice = (i => i.Status == "PAID");
but why does the query provider just silently ignore the Func? When I make the same mistake with other LINQ providers (e.g. Entity Framework) it throws an error.
Yeah, the linq provider in the XeroAPI.Net library is a little ropey - purely because it's the first and only one I've ever written. There's a unit test class called ApiQueryTests in the solution that shows the linq expressions that I've tested for. I know that I've not written any tests for passing a Func variable into the linq expression.
I'll have a look at this in the next few days. In the meantime, you're welcome to write a failing test in the ApiQueryTests class, and write a fix for it. I'm happy to take any pull requests that add value to the library.
Dan..