C# 从对象列表中创建逗号分隔的列表

C# 从对象列表中创建逗号分隔的列表,c#,list,csv,C#,List,Csv,我有个人类型的对象列表。这个类有很多属性,我需要它们都放在一个逗号分隔的列表中,这样我可以在以后的Csv文件中使用它 我已经用foreach管理了它,并添加了每个属性,用逗号手册将其分隔,等等 const string commaSeparator = ","; foreach (var item in individualsInformation) { csv.AppendLine(item.ReferenceNumber + commaSeparator + item.FirstNa

我有个人类型的对象列表。这个类有很多属性,我需要它们都放在一个逗号分隔的列表中,这样我可以在以后的Csv文件中使用它

我已经用foreach管理了它,并添加了每个属性,用逗号手册将其分隔,等等

const string commaSeparator = ",";
foreach (var item in individualsInformation)
{
    csv.AppendLine(item.ReferenceNumber + commaSeparator + item.FirstName + commaSeparator +
                   item.Surname + commaSeparator + item.MiddleName + commaSeparator +
                   item.Address1 + commaSeparator + item.Address2 + commaSeparator + 
                   item.Address3 + commaSeparator + item.Address4 + commaSeparator + 
                   item.City + commaSeparator + item.PostalCode + commaSeparator +
                   item.Country + commaSeparator + item.DateOfBirth.ToString() + commaSeparator +
                   item.ID + commaSeparator + item.Gender + commaSeparator +
                   item.Component + commaSeparator + item.NationalID + commaSeparator + 
                   item.SubSystemID + commaSeparator + item.System);
}
然后我意识到有很多有效的方法,通过使用string.Join

这当然行不通:

string joined = string.Join(",", listOfPersons);
如果我选择这样的属性:

string joined = string.Join(",", listOfPersons(x => x.Id);
当然,我只得到该属性的逗号分隔列表


有没有更有效的方法让每个属性以逗号分隔?

超越了Person类的
ToString
-方法

public class Person
{
    //properties...

    public override string ToString()
    {
        return string.Join(",",
            this.ReferenceNumber,
            this.FirstName,
            this.Surname,
            this.MiddleName,
            this.Address1,
            this.Address2,
            this.Address3,
            this.Address4,
            this.City,
            this.PostalCode,
            this.Country,
            this.DateOfBirth.ToString(),
            this.ID,
            this.Gender,
            this.Component,
            this.NationalID,
            this.SubSystemID,
            this.System);
    }
}
因此,您可以使用
Person.ToString()
生成csv。与反射方法不同,您可以轻松

  • 维持秩序
  • 处理每种不同的类型(
    BirthDate.ToString(“d”)
    Price.ToString(“F2”)
  • 跳过不需要的值,例如
    System.Collections.Generic.List1[System.String]
  • 将值更改为您的需求(
    有子项?“是”:“否”
    • 反射(有时)是你的朋友:

      var props = item.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); // todo: cache & filter not-needed props)
      
      var itemStr = string.Join(", ", 
                           props.Select(p => p.GetValue(item, null)?.ToString())
                                .ToArray());
      

      如果可能的话,我会避免反思

      您可以通过编译时安全性和可读代码轻松实现:

      IEnumerable<string> personTexts = listOfPersons
          .Select(p => String.Join(",", p.ReferenceNumber, p.FirstName, p.Surname, ...));
      string joined = String.Join(Environment.NewLine, personTexts);
      
      IEnumerable personTexts=listOfPersons
      .Select(p=>String.Join(“,”,p.ReferenceNumber,p.FirstName,p.姓氏,…);
      string Join=string.Join(Environment.NewLine,personTexts);
      

      您可以完全控制应该使用哪个属性以及使用的顺序。

      使用反射,其中T:class

      var valueLines=reportData.Select(row=>string.Join(“,”,header.Split(“,”).Select(a=>row.GetType().GetProperty(a).GetValue(row,null))

      乙二醇

      private MemoryStream generateeExcelStream(列表报表数据)
      {
      变量行=新列表();
      var header=“”;
      var attFilter=新节点显示报告属性();
      PropertyDescriptorCollection属性=TypeDescriptor.GetProperties(typeof(T));
      foreach(属性中的PropertyDescriptor属性)
      如果(!prop.Attributes.Matches(attFilter))
      标题+=项目名称+“,”;
      header=header.Substring(0,header.Length-1);
      行。添加(标题);
      var valueLines=reportData.Select(row=>string.Join(“,”,header.Split(“,”).Select(a=>row.GetType().GetProperty(a).GetValue(row,null));
      行。添加范围(valueLines);
      MemoryStream MemoryStream=新的MemoryStream();
      TextWriter tw=新的StreamWriter(memoryStream);
      lines.ForEach(x=>tw.WriteLine(x));
      tw.Flush();
      返回内存流;
      }
      
      停止尝试重新发明轮子。检查答案你目前的方法不全是坏的,而且是完全控制一切的唯一方法。反思完全有帮助。谢谢!:)@nopeflow@HenkHolterman这可能取决于OP的用例。例如,如果该类仅用于生成csv文件,我将覆盖它。我只是想指出,“财产”方式并不总是最好的。代码更少,但灵活性也更低。
      private MemoryStream GenerateExcelStream(List<T> reportData)
          {
              var lines = new List<string>();
              var header = "";
              var attFilter = new NoDisplayInReportAttribute();
              PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T));
              foreach (PropertyDescriptor prop in properties)
                  if (!prop.Attributes.Matches(attFilter))
                      header += prop.Name + ",";
              header = header.Substring(0, header.Length - 1);
              lines.Add(header);
              var valueLines =reportData.Select(row => string.Join(",", header.Split(',').Select(a => row.GetType().GetProperty(a).GetValue(row, null))));
              lines.AddRange(valueLines);
              MemoryStream memoryStream = new MemoryStream();
              TextWriter tw = new StreamWriter(memoryStream);
              lines.ForEach(x => tw.WriteLine(x));
              tw.Flush();
              return memoryStream;
          }