Dapper.Contrib:如何通过对ID以外的列进行过滤来获取行? [英] Dapper.Contrib: How to get a row by filtering on column other than ID?

查看:53
本文介绍了Dapper.Contrib:如何通过对ID以外的列进行过滤来获取行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的课如下:

[Table("tblUser")]
public class User
{
    [Key]
    public int Id { get; set; }
    public string Title { get; set; }
}

使用Dapper.Contrib,是否有一种方法可以通过 Title 而不是ID来获取 User 记录?

Using Dapper.Contrib, is there a way to get the User record by Title instead of Id?

如果我像下面这样查询,它可以工作.但是,我想过滤不是关键字的 Title 列.

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);

推荐答案

查看

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.

使用 GetAll ,您可以进一步使用linq对其进行过滤.但是请记住,这不是在RDBMS上执行的.它将在应用程序端或内存中执行.这意味着,将首先加载整个数据,然后将其过滤.

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.

我个人将针对这种特定情况选择使用Dapper(绕过Contrib).该项目的其他部分仍将使用Contrib.

Personally, I will choose to use Dapper (bypassing Contrib) for such specific scenario. Other part of the project will still use Contrib.

这篇关于Dapper.Contrib:如何通过对ID以外的列进行过滤来获取行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆