I have been using dapper for one of my project and in one case I wanted to return a tuple of primitive data types.(only a single row) Since I used dapper, I really would like to use dapper for this case also. I checked these links but couldn't find solution
我一直在为我的一个项目使用dapper,在一种情况下我想返回一个原始数据类型的元组。(只有一行)因为我使用了dapper,我真的很想在这种情况下使用dapper。我检查了这些链接但找不到解决方案
Can I map a result to Tuple in Dapper?
我可以将结果映射到Dapper中的Tuple吗?
Dapper multiple objects from one row
从一行中精心设计多个对象
Using Dapper to map more than 5 types
使用Dapper映射超过5种类型
This is what my code looks like,
这是我的代码看起来像,
Using(_dbTransaction = _dbConnection.BeginTransaction()){
var command = new CommandDefinition(
"SELECT ID1,ID2 " +
"FROM table " +
"WHERE ID0 = @ID",
new {ID = 34},_dbTransaction); //34 is just a number
var dataSet = _dbConnection.QueryFirst<Tuple<uint, decimal>>(command);
//assign the retrieved values to properties of orderItem (orderItem is a passed object as a parameter)
orderItem.Id = dataSet.item1;
orderItem.ContainerPrice = dataSet.item2;
}
but this would generate an exception
但这会产生异常
{"A parameterless default constructor or one matching signature (System.UInt32 Id, System.Decimal ContainerPrice) is required for System.Tuple`2[[System.UInt32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Decimal, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] materialization"}
{“System.Tuple`2需要一个无参数的默认构造函数或一个匹配的签名(System.UInt32 Id,System.Decimal ContainerPrice)[[System.UInt32,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089 ],[System.Decimal,mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089]] materialization“}
It would be a great help if can suggest a method or point out where I went wrong
如果可以建议一种方法或指出我出错的地方,那将是一个很大的帮助
1 个解决方案
#1
4
If you want to use a Tuple, you can try this:
如果你想使用元组,你可以试试这个:
[Test]
public void TupleTest()
{
var result = _connection.Query<int, decimal, Tuple<int, decimal>>
("select 1 as Id, 12.99 as ContainerPrice", Tuple.Create, splitOn: "*")
.FirstOrDefault();
Assert.That(result.Item1, Is.EqualTo(1));
Assert.That(result.Item2, Is.EqualTo(12.99));
}
You can also avoid Tuples, and use a dynamic:
你也可以避免元组,并使用动态:
[Test]
public void DynamicTest()
{
var result = _connection.Query<dynamic>
("select 1 as Id, 12.99 as ContainerPrice")
.FirstOrDefault();
Assert.That(result.Id, Is.EqualTo(1));
Assert.That(result.ContainerPrice, Is.EqualTo(12.99));
}
#1
4
If you want to use a Tuple, you can try this:
如果你想使用元组,你可以试试这个:
[Test]
public void TupleTest()
{
var result = _connection.Query<int, decimal, Tuple<int, decimal>>
("select 1 as Id, 12.99 as ContainerPrice", Tuple.Create, splitOn: "*")
.FirstOrDefault();
Assert.That(result.Item1, Is.EqualTo(1));
Assert.That(result.Item2, Is.EqualTo(12.99));
}
You can also avoid Tuples, and use a dynamic:
你也可以避免元组,并使用动态:
[Test]
public void DynamicTest()
{
var result = _connection.Query<dynamic>
("select 1 as Id, 12.99 as ContainerPrice")
.FirstOrDefault();
Assert.That(result.Id, Is.EqualTo(1));
Assert.That(result.ContainerPrice, Is.EqualTo(12.99));
}