interface TableProps<T extends Record<string, unknown>> {
entities: T[] | null; // тут возможно еще стоит сделать это поле необязательным?
}
const Table = function <T extends Record<string, unknown>>({
entities,
}: TableProps<T>) {
// ...
return (<>
{entities?.map((entity, key) => {
return Object.entries(entity).map(([key, value], index) => {
// ...
});
})}
</>);
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var treeView = new TreeView
{
Dock = DockStyle.Fill
};
this.Controls.Add(treeView);
var dt = GetDataTable();
PopulateTreeViewFromDataTable(treeView, dt);
}
private DataTable GetDataTable()
{
var dt = new DataTable("Цемент");
dt.Columns.AddRange(new[] {
new DataColumn("Name"),
new DataColumn("NameOfCement")
});
dt.Rows.Add("Портланд", "Вихрь");
dt.Rows.Add("Романцемент", "Тайга");
dt.Rows.Add("Романцемент", "Свой");
dt.Rows.Add("Алумацемент", "Курон");
return dt;
}
public void PopulateTreeViewFromDataTable(TreeView tv, DataTable dataTable)
{
var data =
dataTable
.AsEnumerable()
.Select(row => new
{
Name = row.Field<string>("Name"),
Cement = row.Field<string>("NameOfCement")
})
.GroupBy(x => x.Name);
tv.Nodes.Clear();
var root = tv.Nodes.Add(dataTable.TableName);
foreach (var gr in data)
{
var node = root.Nodes.Add(gr.Key);
foreach (var subItem in gr)
{
node.Nodes.Add(subItem.Cement);
}
}
}
}
public class Book
{
public string Name { get; set; }
public string Author { get; set; }
}
public class Library
{
private List<Book> books;
public Library()
{
books = new List<Book>();
}
public IEnumerable<Book> GetAllBooks {
get
{
return books;
}
}
public void AddBook(Book b)
{
books.Add(b);
}
}
class Program
{
static void Main(string[] args)
{
var library = new Library();
library.AddBook(new Book { Author = "Пушкин", Name = "Руслан и Людмила" });
library.AddBook(new Book { Author = "Булгаков", Name = "Мастер и Маргарита" });
foreach(var book in library.GetAllBooks)
{
Console.WriteLine($"{book.Author} {book.Name}");
}
Console.ReadLine();
}
}