I have two models in my data access layer: Table1 and Table2.
I want to use the WinUI 3 DataGrid from the CommunityToolkit to display two columns from each table: Table1.ColumnA, Table1.ColumnB, Table2.ColumnC, Table2.ColumnD
My thought was to use linq in my ViewModel class to join the enumerable from each model:
IEnumerable<Table1> table1 = unitOfWorkDbGlobal.Table1.GetAll().ToList();
IEnumerable<Table2> table2 = unitOfWorkDbGlobal.Table2.GetAll().ToList();
var JoinedTables = (from t1 in table1
join t2 in table2 on t1.TestGuid equals t2.TestGuid
select new
{ t1.ColumnA, t1.ColumnB,
t2.ColumnC, t2.ColumnD });
The problem that occurred with this approach is that I could create a CommunityToolkit.Mvvm [ObservableProperty]
with table1 or table2 as needed, but I cannot create an observable property with the join because I'm using a var type. When I use JoinedTables.GetType().Name
to determine the explicit type, it returns an Enumerable<JoinIterator>d__122 4
type, which appears to be computer gobbledygook unusable as a property type.
[ObservableProperty]
private ObservableCollection<Table1>? _table1Collection; //this works
[ObservableProperty]
private Enumerable<JoinIterator> d__122`4 _joinedTables; //Errors
How can the joined table be made into an ObservableProperty that can be bound in XAML to a CommunityToolkit DataGrid.
Here is an example of the XAML that I would like to use (note ViewModel
is assigned in the code-behind as the class with the code that I added above):
<controls:DataGrid x:Name="MyDataGrid"
AutoGenerateColumns="False"
ItemsSource="{x:Bind ViewModel.JoinedTables, Mode=OneWay}">
<controls:DataGrid.Columns>
<controls:DataGridTextColumn
Header="Column A"
Width="250"
Binding="{Binding ColumnA}"
FontSize="14" />
<controls:DataGridTextColumn
Header="Column B"
Width="250"
Binding="{Binding ColumnB}"
FontSize="14" />
<controls:DataGridTextColumn
Header="Column C"
Width="250"
Binding="{Binding ColumnC}"
FontSize="14" />
<controls:DataGridTextColumn
Header="Column D"
Width="250"
Binding="{Binding ColumnD}"
FontSize="14" />
</controls:DataGrid.Columns>
</controls:DataGrid>