In most cases, I use a simple observable collection in C# and fill it with data fetched from a SQL query.
Something like this works:
public class MyClass
{
public string mystring1 { get; set; } = String.Empty;
public string mystring1 { get; set; } = String.Empty;
}
ObservableCollection<MyClass> MyClassData = new ObservableCollection<MyClass>();
I can easily insert data fetched by SqlDataReader:
while (dataReader.Read())
{
MyClassData.Add(new MyClass{ mystring1 = dataReader.GetValue(0).ToString(), mystring2 = dataReader.GetValue(1).ToString() }});
}
As it turned out, I need a 3rd member in my class and it should be not a simple string but a list, so I wrote this:
public class MySubClass
{
public string mystring3 { get; set; }
}
public class MyClass
{
public string mystring1 { get; set; } = String.Empty;
public string mystring1 { get; set; } = String.Empty;
public List<MySubClass> MySubClassData { get; set; }
}
Even its only a simple string in MySubClass, I need this as list for later use.
And now I have the big problem to find the right syntax how to get the SQL data into the new structure and all I get is a bunch of curious errors while compiling.
I just don't know how to address the mystring3 in mySubClass as member of MyClass to store data in it.
Please give me a hint.
Thanks,
Hans
public List<MySubClass> MySubClassData { get; } = [];might solve most of that (which means the list is a get-only property, initialized to an empty list). Without knowing the structure of your query, it is hard to advise on how to populate that list - because it is no longer necessarily strictly 1:1 rows to objectsmystring3, return everything in one query and then on client side deduplicate records and fill collections. Currently it is effectively done by EF Core andlinq2db, otherwise you have to do that manually. If you use Dapper here is the sample