C# 如何在消息框中显示列表中的项目?

C# 如何在消息框中显示列表中的项目?,c#,list,struct,messagebox,C#,List,Struct,Messagebox,我正在做一个项目,需要显示收入高于平均水平的人的名单。源数据是一个列表(id是该人员的唯一id): 下面是在消息框中显示所需信息的代码。(此处也添加了高于平均值的列表。) 现在,问题是:我看到的不是消息框中的值列表,而是System.Collections.Generic.list`1[System.String]高于平均水平的人的ID和收入应该在哪里。有人能告诉我我做错了什么,以及如何在消息框中显示列表值吗?StringBuilder是一种选择: StringBuilder above

我正在做一个项目,需要显示收入高于平均水平的人的名单。源数据是一个
列表
id
是该人员的唯一id):

下面是在消息框中显示所需信息的代码。(此处也添加了
高于平均值的
列表。)


现在,问题是:我看到的不是消息框中的值列表,而是
System.Collections.Generic.list
`
1[System.String]
高于平均水平的人的ID和收入应该在哪里。有人能告诉我我做错了什么,以及如何在消息框中显示列表值吗?

StringBuilder是一种选择:

    StringBuilder aboveAverage = new StringBuilder();

    //Determine poverty.
     if (surveyStruct.income - 3480 * surveyStruct.household <= 6730)
    {
        belowAverage += 1;
    }
    else if (surveyStruct.income - 3480 * surveyStruct.household >= 6730)
    {
        aboveAverage.Append(string.Format("id: %s, income: %s\n",
                surveyStruct.id, surveyStruct.income.ToString());
    }
如果您以高于平均水平的列表形式离开,则可以使用join进行此操作,如下所示:

    MessageBox.Show("Your Entry:\nID Code: " + surveyStruct.id + "\nHousehold: " + surveyStruct.household.ToString() + " people\nIncome: " + surveyStruct.income.ToString("C") + "\n\nPeople Above Average:\n" + aboveAverage.ToString() + "\n\nAnd " + belowAveragePercent + "% of people are below average.");
 string.Join(aboveAverage,Environment.NewLine);
public override void string ToString()
{
  return string.Format("The id is {0}, the household is {1} and the income is {2}.", id, household, income);
}
在您当前的代码中——但这看起来不太好

你也可以用Linq来做,你想看看吗

好的,这是一个性感的单线版本:(所有问题都应该有一个单线linq答案):

(使用和缩进不算数,它们只是为了让代码更可读!)

使用NL=Environment.NewLine;
    
字符串缩进=”;
MessageBox.Show(
“您的条目:”+NL+
“ID代码:”+surveyStruct.ID+NL+
“Househouse:”+surveyStruct.Househouse.ToString()+“people”+NL+
“收入:”+surveyStruct.Income.ToString(“C”)+NL+NL+
“高于平均水平的人:”+NL+
缩进+字符串连接(NL+indent,
调查列表,其中(s=>(s.收入-3480)*s.家庭>=6730)
.Select(s=>“ID:+s.ID+“$”+s.income.ToString.ToArray())+NL+

“和”+(调查列表,其中(s=>((s.income-3480)*s.household)首先,制作一份高于平均水平的
列表,并将匹配的收入数据添加到该列表中

然后,需要为自定义结构定义一个
ToString
,如下所示:

    MessageBox.Show("Your Entry:\nID Code: " + surveyStruct.id + "\nHousehold: " + surveyStruct.household.ToString() + " people\nIncome: " + surveyStruct.income.ToString("C") + "\n\nPeople Above Average:\n" + aboveAverage.ToString() + "\n\nAnd " + belowAveragePercent + "% of people are below average.");
 string.Join(aboveAverage,Environment.NewLine);
public override void string ToString()
{
  return string.Format("The id is {0}, the household is {1} and the income is {2}.", id, household, income);
}
然后,在你的MessageBox.Show call中,你需要用

aboveAverage.Aggregate((a,b) => a.ToString() + Enviroment.NewLine + b.ToString())
应该让它正确地显示出来


很抱歉设置了格式,我在移动电话上。

在您问题的最后,您会问:我如何在消息框中显示
列表?

因此,问题的核心是将值列表转换为字符串,以便将该字符串作为参数传递给
MessageBox.Show()

LINQ扩展方法为这个问题提供了一个理想的解决方案。假设您的
列表
看起来像这样(为了简洁起见,我省略了
家庭
字段):

var收入=新列表(){
新国际贸易术语解释通则(“abc0123”,15500),
新IncomeData(“def4567”,12300),
新国际贸易术语解释通则(“ghi8901”,17100)
};
以下LINQ查询将该
列表
转换为
字符串

string消息=收入。
选择(inc=>inc.ToString()。
聚合((buffer,next)=>buffer+“\n”+next.ToString());

要消除调用
Select()
,您可以改用的双参数版本。此方法还允许您将标题指定为累加器的种子值:

string message2=收入。
聚合(
“人均收入数据:”,
(buffer,next)=>buffer+“\n”+next.ToString());
这相当于以下参数类型已显式化的情况:

string message = incomes.
    Aggregate<IncomeData, string>(
        "Income data per person:",
        (string buffer, IncomeData next) => buffer + "\n" + next.ToString());
演示计划

使用系统;
使用System.Collections.Generic;
使用System.Linq;
名称空间LinqAggregateDemo
{
公共课程
{
公共静态void Main(字符串[]args)
{            
var收入=新列表(){
新国际贸易术语解释通则(“abc0123”,15500),
新IncomeData(“def4567”,12300),
新国际贸易术语解释通则(“ghi8901”,17100)
};
字符串消息=收入。
选择(inc=>inc.ToString()。
聚合((buffer,next)=>buffer+“\n”+next.ToString());
Console.WriteLine(“人均收入数据:\n”+消息);
}
公共结构IncomeData
{
公共只读字符串Id;
公共只读int收入;
公共收入数据(字符串id,整数收入)
{
这个.Id=Id;
这个。收入=收入;
}
公共重写字符串ToString()
{
返回字符串格式(
“Id:{0},收入:{1}”,
这个,身份证,,
(a)收入);
}
}
}
}

@Hogan如果我用Linq会有什么不同吗?@chotto座右铭-只有一行!!
string message = incomes.
    Aggregate<IncomeData, string>(
        "Income data per person:",
        (string buffer, IncomeData next) => buffer + "\n" + next.ToString());