如果使用 Dapper 查询时不需要参数怎么办?

发布于 2024-12-01 01:00:46 字数 623 浏览 2 评论 0原文

我有一个查询,它可以在不需要参数的情况下进行计数/分组(没有 where 子句)。

使用 dapper 运行无参数查询的语法是什么?

var _results = _conn.Query<strongType>("Select Count(columnA) as aCount, ColumnB, ColumnC from mytable group by ColumnB, ColumnC");

不起作用。

我尝试了几种不同的方法,但仍然不断收到“ArgumentNullException 未由用户代码处理”。

试图自己弄清楚,到处搜索,我放弃了。提前致谢。

编辑:下面是 SqlMapper.cs 中引发错误的代码行。第 1334 行

il.Emit(OpCodes.Newobj, typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null));

错误详细信息:值不能为空。参数名称:con

I have one query that does a count/group by where I don't need a parameter (there is no where clause).

What is the syntax to run a parameterless query with dapper?

var _results = _conn.Query<strongType>("Select Count(columnA) as aCount, ColumnB, ColumnC from mytable group by ColumnB, ColumnC");

does not work.

I've tried it a few different ways but I still keep getting "ArgumentNullException was unhandled by user code".

Tried to figure it out myself, searched all over and I'm giving up. Thanks in advance.

Edit: Below is the line of code from SqlMapper.cs that throws the error. It's line 1334

il.Emit(OpCodes.Newobj, typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null));

The error details: Value cannot be null. Parameter name: con

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

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

发布评论

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

评论(4

把梦留给海 2024-12-08 01:00:46

将单个结果映射回来效果很好:

var a = cnn.Query<int>("select 1").Single()
// a is 1

如果您的查询以某种方式没有返回结果,您可能会遇到麻烦,例如:

select count(Id) from 
(
select top 0 1 as Id, 2 as Title
) as X
group by Title

返回 0 个结果,因此在空结果集上执行 Single 是行不通的。

Mapping a single result back works just fine:

var a = cnn.Query<int>("select 1").Single()
// a is 1

You may face trouble if somehow your query returns no results, for example:

select count(Id) from 
(
select top 0 1 as Id, 2 as Title
) as X
group by Title

return 0 results, so doing a Single on an empty result set is not going to work.

我为君王 2024-12-08 01:00:46

尝试

var _results = _conn.Query("Select columnB, Count(columnA) C from mytable group by columnB");

int ColumnB = ((int)_results[0].ColumnB);
int C = ((int)_results[0].C);

try

var _results = _conn.Query("Select columnB, Count(columnA) C from mytable group by columnB");

int ColumnB = ((int)_results[0].ColumnB);
int C = ((int)_results[0].C);
菊凝晚露 2024-12-08 01:00:46

值不能为空。参数名称: con

这是由包括 Dappper、PetaPoco 和 Massive 在内的多个动态 ORM 引发的错误,但通常是相同的问题:确保您在不想包含的属性上使用 [Ignore] 属性。这包括从基类继承的属性。该错误毫无用处,但这就是它的意思。

Value cannot be null. Parameter name: con

This is error is thrown by several dynamic ORMs including Dappper, PetaPoco and Massive but it's usually the same problem: Make sure you're using the [Ignore] attribute on the properties you don't want to include. This includes properties inherited from base classes. The error is useless but that's what it means.

时光沙漏 2024-12-08 01:00:46

出现此错误的原因是您尝试在返回对象中设置的属性是仅获取。当然,Dapper 需要能够设置所有属性。您可能会考虑拥有一个单独的数据库 DTO 对象,然后在从数据库读取数据后将其转换为正确的不可变域对象。

将此:更改

public string MyProperty { get; }

为:

public string MyProperty { get; set; }

This error can occur because a property you're trying to set in your return object is get-only. Dapper, of course, requires being able to set all properties. You might consider having a separate database DTO object that then gets converted to your properly immutable domain object after reading from the database.

Change this:

public string MyProperty { get; }

to this:

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