oysteinkrog / SQLite.Net-PCL

Simple, powerful, cross-platform SQLite client and ORM - Updated version with PCL support
MIT License
353 stars 162 forks source link

Expressions passed down to repository method are not properly compiled #301

Open xplatsolutions opened 8 years ago

xplatsolutions commented 8 years ago

Reposting this StackOverflow question in seek of something that I miss so that I don't have to write my own LINQ providers as answered in the comment from usr.

I have the following call in my ViewModel.

Order order= await DataService.Orders.GetAsync(p => p.Id == Guid.Parse("07fafcd9-10db-e511-848d-005056b94716"));

The Orders repository has the following method.

public async Task<T> GetAsync(Expression<Func<T, bool>> predicate)
{
    return await Db.FindAsync<T>(predicate).ConfigureAwait(false);
}

Db property is of type SQLiteAsyncConnection. The predicate I provided above will translate to the below representation.

{p => (p.Id == Parse("07fafcd9-10db-e511-848d-005056b94716"))}

The above predicate will throw an exception with the following message

no such function: parse

So I'm passing the Guid.Parse function down which doesn't exist, similar exception it will be thrown if I have the following predicate.

Order order= await DataService.Orders.GetAsync(p => p.Id == anotherInstanceObject.Id);

The solution is to replace my code with passing the actual value.

Guid guid = Guid.Parse("07fafcd9-10db-e511-848d-005056b94716");
Order order= await DataService.Orders.GetAsync(p => p.Id == guid);

But the above is so counter intuitive and will introduce a lot of problems when developers will start using the DataService class.

What am I missing to make the predicate correctly compile down to my repository method?