简体   繁体   English

无法将“DapperRow”类型的对象转换为“System.String”类型

[英]Unable to cast object of type 'DapperRow' to type 'System.String'

I'm using Dapper to access database objects.我正在使用 Dapper 访问数据库对象。

So this line所以这一行

Console.Write(string.Join(System.Environment.NewLine, results));

Gives me给我

{DapperRow, VALUE_A = 'Y', VALUE_B = 'Y'}

But i'd like to access or print these values like但我想访问或打印这些值,如

DapperRow, 
VALUE_A = 'Y'
VALUE_B = 'Y'

But I am getting "System.InvalidCastException: Unable to cast object of type 'DapperRow' to type 'System.String'" with the following code.但我收到“System.InvalidCastException: Unable to cast object of type 'DapperRow' to type 'System.String'” 使用以下代码。 Where i am trying to use foreach to loop through this list.我正在尝试使用 foreach 来循环遍历此列表。

using (var conn = new OracleConnection("Password=xxxxx;Persist Security Info=True;User ID=xxxxx;Data Source=xxxxx"))
{
// dapper adds an extension method to the connection for querying
string sql = "SELECT X, Y FROM Z WHERE ABC = 123";
var results = conn.Query(sql).ToList();
Console.Write(string.Join(System.Environment.NewLine, results));

    foreach (string value in results)
    {
    Console.WriteLine("> {0}",value);
    }
}

Actually this gives me the result i want..其实这给了我我想要的结果..

using (var conn = new OracleConnection("Password=xxxxx;Persist Security Info=True;User ID=xxxxxxx;Data Source=xxxxxx"))
{
string sql = "SELECT X, Y FROM Z WHERE ABC = 123";
var orderDetail = conn.Query(sql).FirstOrDefault();

    foreach (var pair in orderDetail)
    {
        Console.WriteLine("{0} = {1}", pair.Key, pair.Value);
    }
}

Output: 
VALUE_A = 'Y'
VALUE_B = 'Y'

Then with something like this i can get a value of 'Y' returned.然后用这样的东西我可以得到一个'Y'的值。

if (pair.Key == "VALUE_B")
{
Console.WriteLine("Val {0}", pair.Value);
}

Citing the manual as reference:引用手册作为参考:

string sql = "SELECT * FROM Invoice WHERE InvoiceID = @InvoiceID; SELECT * FROM InvoiceItem WHERE InvoiceID = @InvoiceID;";

using (var connection = My.ConnectionFactory())
{
    connection.Open();

    using (var multi = connection.QueryMultiple(sql, new {InvoiceID = 1}))
    {
        var invoice = multi.Read<Invoice>().First();
        var invoiceItems = multi.Read<InvoiceItem>().ToList();
    }
}

would mean that your code would change to something like将意味着您的代码将更改为类似

        string sql = "SELECT X, Y FROM Z WHERE ABC = @ABC";
        using (var conn = new OracleConnection("Password=xxxxx;Persist Security Info=True;User ID=xxxxx;Data Source=xxxxx"))
        using (var multi = conn.QueryMultiple(sql, new {ABC = 123}))
        {
            var results = multi.Read<Z>().ToList();
            Console.Write(string.Join(System.Environment.NewLine, results.Count));
            foreach (string value in results)
            {
                Console.WriteLine("> X:{0} Y:{1}",value.X, value.Y);
            }
        }

This is late but I just solved a similar issue I was facing and thought to share.这已经很晚了,但我刚刚解决了我面临并想分享的类似问题。 With Dapper, you can transform object into the needed type on Query.使用 Dapper,您可以将对象转换为 Query 所需的类型。 So write a class with the properties X, Y所以写一个具有 X, Y 属性的类

class DataReturned
{
public string X {get; set;}
public string Y {get; set;}
}

Then when querying you do this然后在查询时执行此操作

var results = conn.Query<DataReturned>(sql).ToList();

instead of this而不是这个

var results = conn.Query(sql).ToList();

This will remove the DapperRow and give you just the list you can use.这将删除 DapperRow 并为您提供可以使用的列表。 You may not need the class if you only want one value which you cast to a primitive type如果您只想要一个转换为原始类型的值,则可能不需要该类

var results = conn.Query<string>(sql).ToList();

Am sure you can even cast to string array, dictionary or any other type, just have not experimented that.我相信你甚至可以转换为字符串数组、字典或任何其他类型,只是还没有尝试过。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型“ System.String”强制转换为类型“ System.Object” - Unable to cast the type 'System.String' to type 'System.Object' 无法将“System.string”类型的对象转换为“....Event”类型 - Unable to cast object of type 'System.string' to type '....Event' 无法将“AttachmentDetailsDto”类型的对象转换为“System.String”类型 - Unable to cast object of type 'AttachmentDetailsDto' to type 'System.String' 无法将类型为“ System.String”的对象转换为类型为“ FileItem”的对象 - Unable to cast object of type 'System.String' to type 'FileItem' 无法将类型类的对象强制转换为“ System.String”类型 - Unable to cast object of type class to type 'System.String' 无法将“System.String”类型的对象转换为“System.Collections.Generic.List`1[System.String]”类型 - Unable to cast object of type 'System.String' to type 'System.Collections.Generic.List`1[System.String]' C# 中的 MS Word 自动化 - 无法将类型为“System.String[*]”的 object 转换为类型“System.String[]” - MS Word Automation in C# - Unable to cast object of type 'System.String[*]' to type 'System.String[]' 无法将对象类型“ System.String [*]”转换为类型“ System.String []” - Unable To Cast object type 'System.String[*]' to type 'System.String[]' 无法将类型为“ TextmlBatchDeleteDocument”的对象转换为“ System.String” - Unable to cast object of type 'TextmlBatchDeleteDocument' to 'System.String' 如何解决“无法在CallBimlScript中强制转换&#39;System.String&#39;类型的对象” - How to troubleshoot “Unable to cast object of type 'System.String'” in CallBimlScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM