如何让 dapper 将 int 映射到布尔属性

发布于 2024-12-19 08:09:18 字数 1497 浏览 0 评论 0原文

我有这个类...

public class MyDTO
{
    public int Id { get; set; }

    public bool Selected { get; set; }
}

然后使用 dapper 尝试创建一个像这样的列表...

            var list = this.db.OpenConnection().Query<MyDTO>(
            @"SELECT T1.id, T2.id IS NOT NULL AS selected
            FROM     table1 T1
            LEFT
            JOIN     table2 T2 
            ON   T2.id = T1.id
            AND  Tl.id = @Id",
            new { Id = id });

它返回这样的结果集...

id  selected
 9         0
10         1
11         1
12         0

但是当执行上面的代码时,我收到一个错误,

[InvalidCastException: Specified cast is not valid.]
Deserialize3d3c9260-abcb-4964-97c1-4a4e66b786d3(IDataReader ) +354

[DataException: Error parsing column 2 (selected=0 - Int64)]
Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader) in     C:\Projects\Web\SqlMapper.cs:1685
Deserialize3d3c9260-abcb-4964-97c1-4a4e66b786d3(IDataReader ) +432
Dapper.<QueryInternal>d__13`1.MoveNext() in C:\Projects\Web\Source\SqlMapper.cs:608
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +327
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
Dapper.SqlMapper.Query(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in C:\Projects\Web\Source\SqlMapper.cs:538

我将创建目前是“翻译”属性,但这是一个不寻常的用例吗?

I have this class...

public class MyDTO
{
    public int Id { get; set; }

    public bool Selected { get; set; }
}

and then use dapper to attempt to create a list like this...

            var list = this.db.OpenConnection().Query<MyDTO>(
            @"SELECT T1.id, T2.id IS NOT NULL AS selected
            FROM     table1 T1
            LEFT
            JOIN     table2 T2 
            ON   T2.id = T1.id
            AND  Tl.id = @Id",
            new { Id = id });

which returns a result set like this....

id  selected
 9         0
10         1
11         1
12         0

But when code above is executed, i get an error

[InvalidCastException: Specified cast is not valid.]
Deserialize3d3c9260-abcb-4964-97c1-4a4e66b786d3(IDataReader ) +354

[DataException: Error parsing column 2 (selected=0 - Int64)]
Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader) in     C:\Projects\Web\SqlMapper.cs:1685
Deserialize3d3c9260-abcb-4964-97c1-4a4e66b786d3(IDataReader ) +432
Dapper.<QueryInternal>d__13`1.MoveNext() in C:\Projects\Web\Source\SqlMapper.cs:608
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +327
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
Dapper.SqlMapper.Query(IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in C:\Projects\Web\Source\SqlMapper.cs:538

I'm going to create a "Translating" property for now, but is this an unusual use case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耶耶耶 2024-12-26 08:09:18

我不确定这是否是最好的方法,但它应该可以解决问题:(

SELECT T1.id, CAST(CASE WHEN T2.id IS NULL THEN 0 ELSE 1 END AS BIT) AS selected
FROM ...

说实话,我不相信您的 T2.id IS NOT NULL AS selected 子句首先是合法的 T-SQL,但如果你说它有效,那么我就相信你的话!)

I'm not sure if this is the best way to do it, but it should do the trick:

SELECT T1.id, CAST(CASE WHEN T2.id IS NULL THEN 0 ELSE 1 END AS BIT) AS selected
FROM ...

(To be honest, I'm not convinced that your T2.id IS NOT NULL AS selected clause is legal T-SQL in the first place, but if you say that it's working then I'll take your word for it!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文