C
C#15mo ago
schwartzmj

❔ ✅ Quick question on classes, intellisense, non-nullable fields

I'm using projection in a LINQ query to select fields to create DTOs:
.Select(r => new RowDto
{
Id = r.Id,
Cells = r.Cells.Select(c => new CellDto
{
Id = c.Id,
ColumnId = c.ColumnId,
RowId = r.Id,
.Select(r => new RowDto
{
Id = r.Id,
Cells = r.Cells.Select(c => new CellDto
{
Id = c.Id,
ColumnId = c.ColumnId,
RowId = r.Id,
If I comment out any of these fields, there is no intellisense telling me that I'm omitting required fields and that field ends up just being null. Why is this the case and how can I get this functionality? I don't want to accidentally miss creating required fields in the future. DTOs:
public class RowDto
{
[Required]
public int Id { get; set; }
[Required]
public ICollection<CellDto> Cells { get; set; }
}

public class CellDto
{
[Required]
public int Id { get; set; }
[Required]
public string Value { get; set; }
[Required]
public int RowId { get; set; }
[Required]
public int ColumnId { get; set; }
[Required]
public string ColumnName { get; set; }
public FlagDto? Flag { get; set; }
}
public class RowDto
{
[Required]
public int Id { get; set; }
[Required]
public ICollection<CellDto> Cells { get; set; }
}

public class CellDto
{
[Required]
public int Id { get; set; }
[Required]
public string Value { get; set; }
[Required]
public int RowId { get; set; }
[Required]
public int ColumnId { get; set; }
[Required]
public string ColumnName { get; set; }
public FlagDto? Flag { get; set; }
}
3 Replies
ChucklesTheBeard
ChucklesTheBeard15mo ago
update to C#11 and use required keyword instead of [Required] annotation I guess
[Required]
public int Id {...}
[Required]
public int Id {...}
->
public required int Id {...}
public required int Id {...}
schwartzmj
schwartzmj15mo ago
thank you! looks like this works
Accord
Accord15mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.