如何创建动态确定属性名称的匿名对象?
给定的值的数组,我想创建一个基于这些值的属性的匿名对象。该属性的名称,那就是只要PN
,其中 N
是数组中值的索引。
Given an array of values, I would like to create an anonymous object with properties based on these values. The property names would be simply "pN"
where N
is the index of the value in the array.
例如,给出
对象[]值= {123,富};
我想创建匿名对象
新{P0 = 123,P1 =富};
我能想到的唯一办法做到这将是对使用开关
或如果
链高达参数来支持一个合理的数字,但我想知道是否有一个更优雅的方式来做到这一点:
The only way I can think of to do this would be to to use a switch
or if
chain up to a reasonable number of parameters to support, but I was wondering if there was a more elegant way to do this:
object[] parameterValues = new object[] { 123, "foo" };
dynamic values = null;
switch (parameterValues.Length)
{
case 1:
values = new { p0 = parameterValues[0] };
break;
case 2:
values = new { p0 = parameterValues[0], p1 = parameterValues[1] };
break;
// etc. up to a reasonable # of parameters
}
背景的
我有一个现有的一套针对数据库执行SQL语句的方法。该方法通常需要一个字符串
SQL语句和 params对象[]
的参数,如果有的话。的理解是,如果查询使用参数,它们将被命名为 @ P0,@ P1,@ P2,等等。
。
I have an existing set of methods that execute sql statements against a database. The methods typically take a string
for the sql statement and a params object[]
for the parameters, if any. The understanding is that if the query uses parameters, they will be named @p0, @p1, @p2, etc.
.
例如:
public int ExecuteNonQuery(string commandText, CommandType commandType, params object[] parameterValues) { .... }
这将被称为是这样的:
db.ExecuteNonQuery("insert into MyTable(Col1, Col2) values (@p0, @p1)", CommandType.Text, 123, "foo");
现在我想用的这个类中小巧玲珑包和揭露小巧玲珑的查询< T>
方法,并在某种程度上这样做那会与现有的方法,例如,一致的是这样的:
Now I would like to use Dapper within this class to wrap and expose Dapper's Query<T>
method, and do so in a way that would be consistent with the existing methods, e.g. something like:
public IEnumerable<T> ExecuteQuery<T>(string commandText, CommandType commandType, params object[] parameterValues) { .... }
但精致小巧的查询< T>
方法接受一个匿名对象的参数值:
but Dapper's Query<T>
method takes the parameter values in an anonymous object:
var dog = connection.Query<Dog>("select Age = @Age, Id = @Id", new { Age = (int?)null, Id = guid });
,导致我对创建匿名对象传递参数小巧玲珑的问题。
leading to my question about creating the anonymous object to pass parameters to Dapper.
使用 DynamicParameter
类通过@Paolo特德斯科的要求添加代码。
Adding code using the DynamicParameter
class as requested by @Paolo Tedesco.
string sql = "select * from Account where Id = @p0 and username = @p1";
dynamic values = new DynamicParameter(123, "test");
var accounts = SqlMapper.Query<Account>(connection, sql, values);
在精致小巧的SqlMapper.cs文件的581行抛出一个异常
throws an exception at line 581 of Dapper's SqlMapper.cs file:
using (var reader = cmd.ExecuteReader())
和异常是的SQLException
:
必须声明标量变量@ P0。
Must declare the scalar variable "@p0".
和检查 cmd.Parameters
地产SHOW没有配置的命令参数。
and checking the cmd.Parameters
property show no parameters configured for the command.
您在不当使用短小精悍,你应该永远需要做到这一点,而不是要么实施 IDynamicParameters
或使用特定极为灵活 DynamicParameters
类。
You are misusing Dapper, you should never need to do this, instead either implement IDynamicParameters
or use the specific extremely flexible DynamicParameters
class.
在特定的:
string sql = "select * from Account where Id = @id and username = @name";
var values = new DynamicParameters();
values.Add("id", 1);
values.Add("name", "bob");
var accounts = SqlMapper.Query<Account>(connection, sql, values);
DynamicParameters
可以在一个匿名类构造函数。您可以Concat的使用 AddDynamicParams
方法 DynamicParameters
。
DynamicParameters
can take in an anonymous class in the constructor. You can concat DynamicParameters
using the AddDynamicParams
method.
进一步,有上不久类型没有严格的依赖关系。小巧玲珑将允许具体类型为PARAMS例如:
Further more, there is no strict dependency on anon-types. Dapper will allow for concrete types as params eg:
class Stuff
{
public int Thing { get; set; }
}
...
cnn.Execute("select @Thing", new Stuff{Thing = 1});
凯文也有类似的问题:的寻找一个快速简便的方法凝聚在POCO 所有属性 - DynamicParameters
完美的作品在这里也无需任何魔法箍跳跃。
Kevin had a similar question: Looking for a fast and easy way to coalesce all properties on a POCO - DynamicParameters
works perfectly here as well without any need for magic hoop jumping.