C# 如何并排绘制两个项目列表

C# 如何并排绘制两个项目列表,c#,.net,C#,.net,我正在尝试绘制一些项目列表,并以这种格式将它们打印到一个文件中 items amount maize $50 meat $100 我试过使用stringbulder private List<sting> _Items; private List<string> _Uprices; //list of items StringBuilder myItems = StringBuilder(); foreach (var item in _Item

我正在尝试绘制一些项目列表,并以这种格式将它们打印到一个文件中

 items   amount

 maize   $50
 meat    $100
我试过使用stringbulder

private List<sting> _Items;
private List<string> _Uprices;

//list of items
StringBuilder myItems = StringBuilder();
foreach (var item in _Items) {
    myItems.AppendLine(item.PadRight (30));
}

//list of prices
StringBulder prices = StringBuilder();
foreach (var item in _Uprices) {
    prices.AppendLine(item.PadRight (30));
}
但我在这里得到的结果很糟糕 如果将foreach项添加到字符串中,则会打印最后一个值。 有人能帮我一下吗

请问我做错了什么,我在谷歌搜索过,找不到答案

更新:

请检查此代码段


我认为这个问题没有被很好地理解。但是我已经用一个指向源代码的链接对其进行了更新,并打印到OneNote。

如果没有更多信息,很难判断,但我认为您希望使用单个for循环创建字符串

private List<sting> _Items;
private List<string> _Uprices;

StringBuilder myItems = StringBuilder();
foreach (var i = 0; i < _Items.Count; i++) {
    myItems.AppendLine(_Items[i].PadRight(30) + _Uprices[i]);
}

Console.Write(myItems);
private List\u项目;
私人名单;
StringBuilder myItems=StringBuilder();
foreach(变量i=0;i<\u Items.Count;i++){
附录行(_Items[i].PadRight(30)+_Uprices[i]);
}
Console.Write(myItems);

您是否尝试过使用词汇表

Dictionary<string, string> dictionary = new Dictionary<string, string>();
Dictionary Dictionary=newdictionary();

为什么不使用字典,只打印带值的键

然后试试这个

int y=20;
    foreach(var keyValuePair in dictionary) {
     g.DrawString(keyValuePair.Key+"\t"+keyValuePair.Value, new Font("Times New Roman",12, new SolidBrush(Color.Black),20,y+=15);
    }

不清楚,因为您说要写入文件,但使用了
图形

假设输出为文件: 使用Linq的
Zip
和C#6.0的字符串插值,您可以这样做:

List<string> _Items = new List<string> { "maize", "meat" };
List<string> _Uprices = new List<string> { "$50", "$100" };

// For prior c# 6 use here string.Format
var lines = _Items.Zip(_Uprices, (item, price) => $"{item}  {price}").ToList();

using (var stream = File.CreateText(@"C:\myfile.txt"))
{
    stream.WriteLine("Items - Amount");
    lines.ForEach(stream.WriteLine);
}

//Output:

// maize  $50
// meat   $100

我仍然建议对数据使用一些其他结构,比如一个具有两个属性的类,而不是两个按索引关联的列表,以便打印到需要使用file and Text Writer的文件中

if (!File.Exists(path))
    File.Create(path);

TextWriter tw = new StreamWriter(path);
tw.WriteLine("items ammount");
tw.Close(); 
为了同时生成这两个东西,这是一种方法

if (_Items.Count == _Uprices.Count)
{
    for (int i = 0; i < _Items.Count; i++)
    {
        tw.WriteLine(String.Format("{0} {1}",_Items[i],_Uprices[i]));
    }
}
if(\u Items.Count==\u Uprices.Count)
{
对于(int i=0;i<\u Items.Count;i++)
{
WriteLine(String.Format(“{0}{1}”,_Items[i],_Uprices[i]);
}
}
我不会给出完整的剧本。混合搭配以更好地理解它。

我建议Linq,
Zip
确切地说:

要绘制所有组合线,请执行以下操作:

string myItems = string.Join(Environment.NewLine, _Items
  .Zip(_Uprices, (item, price) => $"{item}  {price}"));

g.DrawString(myItems, ...);
如果要将这些行放到文件中:

string[] result = _Items
  .Zip(_Uprices, (item, price) => $"{item}  {price}")
  .ToArray();

...

// draw all the lines in one go 
g.DrawString(string.Join(Environment.NewLine, result) ...); 

...
// draw line after line (in case you have other lines/text to output)
for (int i = 0; i < result.Length; ++i) {
  g.DrawString(result[i], ...);
  ... 
}  

...

File.WriteAllLines(@"C:\MyFile.txt", result);
最后,您可以具体化结果(例如,作为数组),并绘制或写入文件:

string[] result = _Items
  .Zip(_Uprices, (item, price) => $"{item}  {price}")
  .ToArray();

...

// draw all the lines in one go 
g.DrawString(string.Join(Environment.NewLine, result) ...); 

...
// draw line after line (in case you have other lines/text to output)
for (int i = 0; i < result.Length; ++i) {
  g.DrawString(result[i], ...);
  ... 
}  

...

File.WriteAllLines(@"C:\MyFile.txt", result);
string[]结果=\u项
.Zip(_Uprices,(项目,价格)=>$“{item}{price}”)
.ToArray();
...
//一次画完所有的线
g、 DrawString(string.Join(Environment.NewLine,result)…);
...
//一行接一行地画(如果要输出其他行/文本)
for(int i=0;i
将它们打印到文件和打印到图形是两件不同的事情。您想要文件还是屏幕?您可以创建字符串数组,然后使用
g.DrawString
方法在foreach循环中逐个打印它们。它不能回答这样的问题;但是,如果物品和价格之间存在相关性,最好的方法是以某种结构或类别将这两个属性存储在一起;然后您可以重写.ToString()以打印或显示列,您通常需要使用固定大小的字体,如Courier或ConsoleAs。除此之外,你需要明确你的实际目标。答案以一种可笑的方式大不相同,因为似乎没有人理解你的问题!可能出现的情况是,
\u项
的项不是唯一的;如果它们是
字典
是一个源代码。我没有尝试写入文件。我使用了此代码段,但输出与其他内容重叠@styflei将尝试使用带有属性的类,然后返回you@EzeSundayEze-我看到了您的更新,您希望打印到OneNote。基本上,使用
的代码替换为您需要的代码,但主要问题是如何格式化行-即
.Zip
:)@EzeSundayEze-我不确定我是否理解。
行中的每个项目都是您想要的字符串。把它放在
g.DrawString(线条,新字体(“Times new Roman”,12),新SolidBrush(颜色.黑色),20,20)谢谢,我会把这个作为我的答案。剩下的我来收拾。谢谢。这是可行的,但与其他内容重叠,我已经更新了,问题的url指向源代码
File.WriteAllLines(@"C:\MyFile.txt", _Items
  .Zip(_Uprices, (item, price) => $"{item}  {price}")); 
string[] result = _Items
  .Zip(_Uprices, (item, price) => $"{item}  {price}")
  .ToArray();

...

// draw all the lines in one go 
g.DrawString(string.Join(Environment.NewLine, result) ...); 

...
// draw line after line (in case you have other lines/text to output)
for (int i = 0; i < result.Length; ++i) {
  g.DrawString(result[i], ...);
  ... 
}  

...

File.WriteAllLines(@"C:\MyFile.txt", result);