问题
My class is like below:
[Table("tblUser")]
public class User
{
[Key]
public int Id { get; set; }
public string Title { get; set; }
}
Using Dapper.Contrib, is there a way to get the User
record by Title
instead of Id?
If I query like below, it works. But, I want to filter on Title
column which is not a key.
await connection.GetAsync<User>(Id);
回答1:
Looking at the documentation, Dapper.Contrib does not support retrieval of records using criteria other than key. In other words, it does not support any kind of predicate system in its current implementation.
Using GetAll
, you can further filter it with linq. But remember that this is not being executed on RDBMS. It will be executed on application side or in memory. That means, entire data will be loaded first and then it will be filtered.
Personally, I will choose to use Dapper (bypassing Contrib) for such specific scenario. Other part of the project will still use Contrib.
来源:https://stackoverflow.com/questions/54454928/dapper-contrib-how-to-get-a-row-by-filtering-on-column-other-than-id