using Microsoft.Data.SqlClient; namespace Library; public class CategoryTreeData { private readonly List m_RootElements = new( ); public List RootElements { get => m_RootElements; } private readonly Dictionary m_AllElements = new( ); public int GetRootID( ) { if ( m_RootElements.Count == 0 ) return 0; return m_RootElements[0].ID; } public CategoryItemData? FindCategory( int id ) { if ( m_AllElements.TryGetValue( id , out CategoryItemData? value ) ) return value; return null; } public void LoadCategoryTree( SqlConnection connection ) { using SqlDataReader reader = new SQLBuilder( connection , "select id,idfat,label,ime_x,cnt,lis_txt from [select_razdeli]" ) .ExecuteReader( ); while ( reader.Read( ) ) { CategoryItemData model = new( ) { ID = reader.Int( "id" ) , ParentID = reader.Int( "idfat" ) , ElementCount = reader.Int( "cnt" ) , Depth = reader.Int( "label" ) , Name = reader.StringSafe( "ime_x" ).Trim( ), Catego_opis = reader.StringSafe( "lis_txt" ).Trim( ) }; m_AllElements[model.ID] = model; } foreach ( CategoryItemData m in m_AllElements.Values ) { if ( m_AllElements.TryGetValue( m.ParentID , out CategoryItemData? parent ) ) parent.Children.Add( m ); else m_RootElements.Add( m ); } } }