I have a simple class:
public class User : ActiveRecordLinqBase<User>
{
[PrimaryKey(Column = "user_id", Length = 20)]
public string Id { get; set; }
[Property(Column = "password", Length = 16)]
public string Password { get; set; }
...
}
and I have created the following repository:
public class SqlRepository<T> : IRepository<T> where T : ActiveRecordLinqBase<T>, new() {
public void Add(T entity) {
entity.SaveAndFlush();
}
public void Remove(T entity) {
entity.DeleteAndFlush();
}
public void Modify(T entity) {
entity.UpdateAndFlush(); ;
}
...
public IEnumerable<T> FindAll(Func<T, bool> predicate) {
return ActiveRecordLinqBase<T>.Queryable.Where(predicate);
}
}
Now, when running the following unit test (against a MySQL database):
[Test]
public void Test_Sample() {
var repo = new SqlRepository<T>();
repo.Add("john.doe", "keyword1");
repo.Add("other.user", "keyword2");
var users = repo.FindAll(x => x.Username.Contains("john")).ToList();
Assert.AreEqual(1, users.Count);
}
... I get the following SQL query:
SELECT this_.user_id as user1_0_0_, this_.password as password0_0_, this_.role as role0_0_ FROM users this_
Where is the WHERE
clause?
If I instead do the following in the same test directly...
var users = User.Queryable.Where(x => x.Username.Contains("john"));
I get the following SQL:
SELECT this_.user_id as user1_0_0_, this_.password as password0_0_, this_.role as role0_0_ FROM users this_ WHERE this_.user_id like ?p0;?p0 = '%john%'
Am I doing something wrong?
What is the difference between those two queries?
Edit: I also tried with
return ActiveRecordLinq.AsQueryable<T>().Where(predicate);
without success.
Now this is just because I like code, and sometimes I notice stuff... I'm no expert on Active Record, so this is just a guess...
Maybe you should change the signature of the FindAll
method from
public IEnumerable<T> FindAll(Func<T, bool> predicate)
into
public IEnumerable<T> FindAll(Expression<Func<T, bool>> predicate)
which will allow you to hit the right overload of Where
, which is most likely the overload you're looking for.
It's because a Func
can't be reflected upon the same way an Expression of Func
can.