C
C#2y 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
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
schwartzmjOP2y ago
thank you! looks like this works
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server