C
C#4w ago
Paleo

Prefered approached when needing to customize Data Grid value

Hi all ! In a blazor project, what is the preferred approach when displaying a data grid, with items from EFCore that contains optional relationships ? For example; I have an entity model "XYZ" that has an optional relationship to model "Boat". In my datagrid, I want to have a column for the value of "XYZ.Boat.Name". However, since the Boat relationship is optional, is it sometime null, and the datagrid throws an exception when it reaches an Item without a Boat relationship, since it can't access "Name". I see two options; - "Prepare" the data for the grid and store the result in a IQueryable object, and use that as the data source instead of "connecting" the grid directly to the models - Add a method in my model class and invoke that instead of the property directly. For example "x => x.GetBoatNameIfItExists()" , which would return a null as the value when there is no Boat relationship. I feel like my model class should be lean and method-less, but this seems to be the better option to me right now...
1 Reply
Mayor McCheese
Use a dto ( data transfer object ) which better represents your end state rather than your entity object.