Display relational property on RadzenDataGrid

Hello!

Programming newbie here.

After retrieving a table called "Letters", which has a RecipientId column (mapped to AspNetUser Id), I would like to relate it to the AspNetUsers table so that I can retrieve the recipient's full name on a RadzenDataGrid column, e.g.:

<RadzenDataGridColumn TItem="Punya.Models.defaultdb.Letter" Property="" Title="Nome" />

What would be the best way to go about this? I'm receiving conflicting answers from AI.

Also, is this earlier step (where I check for an address exact match) logically sound?

string recipient = await UserManager.Users.Where(u => u.Address == address).Select(u => u.Id).FirstOrDefaultAsync();

Thanks in advance!

The grid column shows whatever Property points to on the row object, and it can follow a navigation property, so the cleanest way is to give Letter a reference to its recipient and bind the column to it.

If the database has a foreign key from Letters.RecipientId to AspNetUsers.Id, the generated model already has a navigation property (usually AspNetUser or Recipient). Load it together with the letters and point the column at the nested property:

letters = await dbContext.Letters.Include(l => l.Recipient).ToListAsync();
<RadzenDataGridColumn TItem="Letter" Property="Recipient.FullName" Title="Nome" />

Sorting and filtering work on the nested property too. If the user has separate first and last name columns, keep Property for sorting and filtering and render the text with a Template:

<RadzenDataGridColumn TItem="Letter" Property="Recipient.LastName" Title="Nome">
    <Template Context="letter">
        @letter.Recipient?.FirstName @letter.Recipient?.LastName
    </Template>
</RadzenDataGridColumn>

This is exactly what the column template demo does with Property="Employee.LastName" and Include("Employee"): Customize Cell Content with Column Templates in a Blazor DataGrid | Free Radzen Blazor

If there is no foreign key (common when the Id is only stored as a string), EF cannot navigate, so load the users once and look the name up in the template:

var users = await UserManager.Users.ToDictionaryAsync(u => u.Id, u => u.FullName);
<RadzenDataGridColumn TItem="Letter" Property="RecipientId" Title="Nome">
    <Template Context="letter">
        @(users.TryGetValue(letter.RecipientId, out var name) ? name : letter.RecipientId)
    </Template>
</RadzenDataGridColumn>

Adding the foreign key in the database and regenerating the model is the better long-term option, because then filtering and sorting by recipient name come for free.

About the address lookup: it works, but FirstOrDefaultAsync returns null when no user has that exact address, so check for null before using recipient. Address text is a fragile key (casing, trailing spaces, abbreviations); if you can, store the user's Id on the letter when it is created instead of matching it back from the address later.