minhhungit / ConsoleTableExt

A fluent library to print out a nicely formatted table in a console application C#
MIT License
323 stars 36 forks source link

[Question] Сolumns visibility | Select the row that I want to display #40

Closed OleksandrMyronchuk closed 1 year ago

OleksandrMyronchuk commented 1 year ago

Can you help me display only those rows that I want to

For example: there is the following code:

public class TestClass1
{
    public string A { get; set; }
    public string B { get; set; }
    public string C { get; set; }
}
  List<TestClass1> testClass = new List<TestClass1>();
  testClass.Add(new TestClass1 { A = "a0", B = "b0", C = "c0" });
  testClass.Add(new TestClass1 { A = "a1", B = "b1", C = "c1" });
  ConsoleTableBuilder
  .From(testClass)
  .WithColumn("A", "B")
  .ExportAndWriteLine();

I would like to display only the "A" and "B" columns

How can I do this ?

So the question is: how to display specific columns ?

minhhungit commented 1 year ago

@OleksandrMyronchuk I think you can convert your TestClass1 to another class ex ABClass that just have only field A and B, then instruct ConsoleTableBuilder use ABClass by using .From

  using System.Linq;

  public class TestClass1
  {
      public string A { get; set; }
      public string B { get; set; }
      public string C { get; set; }
  }

  public class ABClass // <=============== new class
  {
      public string A { get; set; }
      public string B { get; set; }
  }

  List<TestClass1> oldList = new List<TestClass1>();
  oldList.Add(new TestClass1 { A = "a0", B = "b0", C = "c0" });
  oldList.Add(new TestClass1 { A = "a1", B = "b1", C = "c1" });

  List<ABClass> newList = new List<ABClass>();
  newList = oldList.Select(x=> new ABClass{A = x.A, B = x.B}).ToList(); // <====== convert to new list

  ConsoleTableBuilder
  .From(newList) // <==================
  .WithColumn("A", "B")
  .ExportAndWriteLine();