Dapper.Contrib: How to get a row by filtering on column other than ID?

纵饮孤独 提交于 2021-02-11 17:02:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!