英文:
.net How to add Add to nested list object
问题
以下是您要翻译的部分:
我有以下类对象
我使用存储过程从我的 sp 中填充 DataTable 和 DebtAllOut 类的消息
我遍历 DataTable 并填充一个列表
public class DebtAllOut
{
public string message { get; set; }
public List<DebtAllDetail> debtalldetail { get; set; }
}
public class DebtAllDetail
{
public int debtid { get; set; }
public string name { get; set; }
}
var debtallout = new DebtAllOut();
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.AddWithValue("@ProfileID", profileid);
DataTable dt = new DataTable();
da.Fill(dt);
debtallout.message = "提供一般消息";
List<DebtSingleOut> lstDebtSingleOut = new List<DebtSingleOut>();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DebtSingleOut debtsingleout = new DebtSingleOut();
if (dt.Rows[i]["RowID"] != DBNull.Value) {
debtsingleout.debtid = Convert.ToInt32(dt.Rows[i]["RowID"]);
}
lstDebtSingleOut.Add(debtsingleout);
}
}
debtallout.debtalldetail.AddRange(lstDebtSingleOut);
最后一行出现错误,当我尝试将列表添加到 debtalldetail 嵌套类时
debtallout.debtalldetail.AddRange(lstDebtSingleOut);
我收到的错误是
错误 CS1503 参数 1:无法从 'System.Collections.Generic.List<mystuff.Models.DebtSingleOut>' 转换为 'System.Collections.Generic.IEnumerable<mystuff.Models.DebtAllDetail>'。
英文:
I have the following class object
I fill a DataTable with data from my sp and the message from the DebtAllOut class
I roll through the DataTable and filling a list
public class DebtAllOut
{
public string message { get; set; }
public List<DebtAllDetail> debtalldetail { get; set; }
}
public class DebtAllDetail
{
public int debtid { get; set; }
public string name { get; set; }
}
var debtallout = new DebtAllOut();
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.AddWithValue("@ProfileID", profileid);
DataTable dt = new DataTable();
da.Fill(dt);
debtallout.message = "Providing general message";
List<DebtSingleOut> lstDebtSingleOut = new List<DebtSingleOut>();
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DebtSingleOut debtsingleout = new DebtSingleOut();
if (dt.Rows[i]["RowID"] != DBNull.Value) {
debtsingleout.debtid = Convert.ToInt32(dt.Rows[i] ["RowID"]);
}
lstDebtSingleOut.Add(debtsingleout);
}
}
debtallout.debtalldetail.AddRange(lstDebtSingleOut);
The last line gives the the error. When I try to add the list to the debtalldetail nested class
debtallout.debtalldetail.AddRange(lstDebtSingleOut);
The error I receive is
Error CS1503 Argument 1: cannot convert from 'System.Collections.Generic.List<mystuff.Models.DebtSingleOut>' to 'System.Collections.Generic.IEnumerable<mystuff.Models.DebtAllDetail>'
答案1
得分: 0
您正在尝试将类型为 List<DebtSingleOut>
的列表 lstDebtSingleOut
添加到类型为 List<DebtAllDetail>
的 debtalldetail
属性中。完整的逻辑不容易理解,但我认为您混淆了 DebtAllDetail
和 DebtSingleOut
类。
您需要为每一行创建一个新的 DebtAllDetail
对象,并将其添加到 debtalldetail
列表中,或者您可以修复代码并将类型更改为必需的类型。
var debtallout = new DebtAllOut();
debtallout.message = "提供一般消息;"
// 初始化新的 List<DebtAllDetail> 实例以添加元素
debtallout.debtalldetail = new List<DebtAllDetail>();
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.AddWithValue("@ProfileID", profileid);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
// 创建并初始化新的 DebtAllDetail 对象
DebtAllDetail debtAllDetail = new DebtAllDetail();
if (dt.Rows[i]["RowID"] != DBNull.Value)
debtAllDetail.debtid = Convert.ToInt32(dt.Rows[i]["RowID"]);
if (dt.Rows[i]["Name"] != DBNull.Value)
debtAllDetail.name = dt.Rows[i]["Name"].ToString(); // 或者使用其他代码检索名称
// 将初始化的 DebtAllDetail 对象添加到列表
debtallout.debtalldetail.Add(debtAllDetail);
}
}
希望这可以帮助您理解和修复代码中的问题。
英文:
You're trying to add a list lstDebtSingleOut
of type List<DebtSingleOut>
to the debtalldetail
property which is of type List<DebtAllDetail>
. It's not easy to understand full logic but I think you mixed up classes DebtAllDetail
and DebtSingleOut
.
You need to create a new DebtAllDetail
object for each row and add it to the debtalldetail
list or you can fix the code change the type to the necessary.
var debtallout = new DebtAllOut();
debtallout.message = "Providing general message;"
// inirialize new instance of List<DebtAllDetail> to add elements
debtallout.debtalldetail = new List<DebtAllDetail>();
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.AddWithValue("@ProfileID", profileid);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
// create and initialize new DebtAllDetail object
DebtAllDetail debtAllDetail = new DebtAllDetail();
if (dt.Rows[i]["RowID"] != DBNull.Value)
debtAllDetail.debtid = Convert.ToInt32(dt.Rows[i]["RowID"]);
if (dt.Rows[i]["Name"] != DBNull.Value)
debtAllDetail.name = dt.Rows[i]["Name"].ToString(); // or another code to retrieve the name
// add initialized DebtAllDetail object to list
debtallout.debtalldetail.Add(debtAllDetail);
}
}
答案2
得分: 0
给定以下的类定义:
public class DebtAllOut
{
public string message { get; set; }
public List<DebtAllDetail> debtalldetail { get; set; }
}
重要的是要理解这一行的结果:
var debtallout = new DebtAllOut();
此时,debtallout.debtalldetail
的值仍然为 null!
这是因为类定义没有指定实际创建列表对象。它只指定创建一个可能(或可能不)引用某个列表变量的变量。也就是说,你有一个引用,但这个引用目前还没有指向任何东西。
你可以通过两种方式来修复它。首先,通过更改类定义:
public class DebtAllOut
{
public string message { get; set; }
public List<DebtAllDetail> debtalldetail { get; private set; } = new List<DebtAllDetail>();
}
(注意使用了 private
。你仍然可以向列表中添加或删除项,因为访问列表的成员,包括像 Add()
这样的方法,仍然是一个 get
操作。)
第二种修复方法是手动创建一个新的列表实例:
var debtallout = new DebtAllOut();
debtallout.debtalldetail = new List<DebtAllDetail>();
但是,除此之外,我们可以显著改进代码:
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.AddWithValue("@ProfileID", profileid);
DataTable dt = new DataTable();
da.Fill(dt);
var debtallout = new DebtAllOut() {
message = "提供一般消息",
debtalldetail = new List<DebtAllDetail>()
};
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["RowID"] != DBNull.Value)
{
DebtSingleOut debtsingleout = new DebtSingleOut() {
debtid = Convert.ToInt32(dt.Rows[i]["RowID"]);
}
debtallout.debtalldetail.Add(debtsingleout);
}
}
(代码中的 "
在这里被翻译为了 "
,<
被翻译为了 <
,>
被翻译为了 >
。)
英文:
Given this class definition:
public class DebtAllOut
{
public string message { get; set; }
public List<DebtAllDetail> debtalldetail { get; set; }
}
It's important to understand the result of this line:
var debtallout = new DebtAllOut();
At this point, the value of debtallout.debtalldetail
is still null!
This is because the class definition did not specify to actually create the list object. It only specified to create a variable that might (or might not) refer to some list variable. That is, you have a reference, but the reference doesn't refer to anything yet.
You can fix it in two ways. First, by changing the class definition:
public class DebtAllOut
{
public string message { get; set; }
public List<DebtAllDetail> debtalldetail { get; private set; } = new List<DebtAllDetail>();
}
(Notice the use of private
. You will still be able to add or remove items from the list, because accessing members of the list, including methods like Add()
, is still a get
operation.)
The second fix is to manually create a new list instance:
var debtallout = new DebtAllOut();
debtallout.debtalldetail = new List<DebtAllDetail>();
But all that aside, we can significantly improve the code:
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.AddWithValue("@ProfileID", profileid);
DataTable dt = new DataTable();
da.Fill(dt);
var debtallout = new DebtAllOut() {
message = "Providing general message",
debtalldetail = new List<DebtAllDetail>()
};
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i]["RowID"] != DBNull.Value)
{
DebtSingleOut debtsingleout = new DebtSingleOut() {
debtsingleout.debtid = Convert.ToInt32(dt.Rows[i]["RowID"]);
}
debtallout.debtalldetail.Add(debtsingleout);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论