gpt4 book ai didi

c# - 结果类型 'System.Tuple` 3[System.Guid,System.Int32,System.String]' 可能不是抽象的,必须包含默认构造函数

转载 作者:行者123 更新时间:2023-11-30 22:51:36 32 4
gpt4 key购买 nike

是否可以从 ObjectContext 对象中读取元组列表?

我在存储过程中有类似这样的数据库查询

SELECT 
T.Id as Item1, -- this is guid
T.WorkflowId AS Item2, -- this is int
T.ActionName AS Item3 -- this is string
FROM
MyTable T

C# 代码我试着这样读

var command = context.Database.Connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "[SEQUOIA].[GetWriteOffRequestDetails]";
var objectContext = ((IObjectContextAdapter)context);
context.Database.Connection.Open();

if (reader.NextResult())
{
// this line is giving error, so basically where it is trying to read/translate the result
List<Tuple<Guid, int, string>> requestItemActions = objectContext.ObjectContext.Translate<Tuple<Guid, int, string>>(reader).Select(x => new Tuple<Guid, int, string>(x.Item1, x.Item2, x.Item3)).ToList();
}

但是它抛出了这个异常

The result type 'System.Tuple`3[System.Guid,System.Int32,System.String]' may not be abstract and must include a default constructor.

那么甚至可以像这样读取元组吗?

如果是,有人能指出我遗漏了什么吗?

最佳答案

在这种情况下你不能使用tuple 因为tuple class没有默认构造函数(无参数构造函数).net 框架使用反射自动创建此类型,因此它应该有默认构造函数。

所以在这种情况下的解决方案是创建包含这三个属性的类并使用它而不是元组

public class DataClass
{

public Guid Item1 { get; set; }
public int Item2 { get; set; }
public string Item3 { get; set; }
}




List<DataClass> requestItemActions = objectContext.ObjectContext.Translate<DataClass>(reader).ToList();

关于c# - 结果类型 'System.Tuple` 3[System.Guid,System.Int32,System.String]' 可能不是抽象的,必须包含默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59031419/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com