How to prevent items in a list returning "0" when not filled in and they supposed to be "null"?

Viewed 81

This was kind of a hard question to ask but this is my problem:

I'm populating a grid with data I obtain from a different class, this class uses a (generic) Model that can represent multiple models:

Model(can represent Vessel or Container):

public class DataGridInstallationRow
{
    [Key]
    public string Name { get; set; }

    //Vessel
    public int IMO { get; set; }
    public int MMSI { get; set; }
    public int EEOI { get; set; }
    public int FOC { get; set; }

    [Display(Name = "Total Fuel Mass")]
    public int TotalFuelMass { get; set; }

    [Display(Name = "Average Speed")]
    public int AverageSpeed { get; set; }

    [Display(Name = "Total Distance Sailed")]
    public int TotalDistanceSailed { get; set; }

    //Container
    [Display(Name = "Generated by Sun")]
    public int EnergyGeneratedBySun { get; set; }
    [Display(Name = "Generated by Wind")]
    public int EnergyGeneratedByWind { get; set; }
    [Display(Name = "Generated by Generator")]
    public int EnergyGeneratedByGenerator { get; set; }
    [Display(Name = "Consumed by EV's")]
    public int EnergyConsumedByEV { get; set; }
    [Display(Name = "Consumed by Construction Site")]
    public int EnergyConsumedByConstructionSite { get; set; }
}

This model is used in my provider:

        if (fleet.Type.Equals("Container"))
        {
            return Enumerable.Range(0, 10).Select(i => new DataGridInstallationRow()
            {
                Name = $"Container {i}",
                EnergyGeneratedBySun = 13,
                EnergyGeneratedByWind = 19,
                EnergyGeneratedByGenerator = 3,
                EnergyConsumedByEV = 15,
                EnergyConsumedByConstructionSite = 24
            }).ToList();
        }
        else
        {
            return Enumerable.Range(0, 10).Select(i => new DataGridInstallationRow()
            {
                Name = $"Vessel {i}",
                IMO = 231,
                MMSI = 1344,
                EEOI = 8121,
                FOC = 123,
                TotalFuelMass = 6817,
                AverageSpeed = 14,
                TotalDistanceSailed = 1560
            }).ToList();
        }

As u can see, depending on the Fleet.Type, one of the other is filled in. If Fleet.Type is container the object will look like this:

enter image description here

As u can see the properties of "Vessel" is filled in aswell with all "0", I want these to be null instead of "0" because my datagrid is filled with both models now:

enter image description here

Whats best practice to avoid and fix this?

UPDATE

Applied solution of Dogac:

if (fleet.Type.Equals("Container"))
        {
            return Enumerable.Range(0, 10).Select(i => new DataGridInstallationRow()
            {
                Name = $"Container {i}",
                EnergyGeneratedBySun = 13,
                EnergyGeneratedByWind = 19,
                EnergyGeneratedByGenerator = 3,
                EnergyConsumedByEV = 15,
                EnergyConsumedByConstructionSite = 24
            }).Where(row =>
            {
                return row.EnergyGeneratedBySun.HasValue &&
                       row.EnergyGeneratedByWind.HasValue &&
                       row.EnergyGeneratedByGenerator.HasValue &&
                       row.EnergyConsumedByEV.HasValue &&
                       row.EnergyConsumedByConstructionSite.HasValue;
            }).ToList();
        }
        else
        {
            return Enumerable.Range(0, 10).Select(i => new DataGridInstallationRow()
            {
                Name = $"Vessel {i}",
                IMO = 231,
                MMSI = 1344,
                EEOI = 8121,
                FOC = 123,
                TotalFuelMass = 6817,
                AverageSpeed = 14,
                TotalDistanceSailed = 1560
            }).Where(row =>
            {
                return row.IMO.HasValue &&
                       row.MMSI.HasValue &&
                       row.EEOI.HasValue &&
                       row.FOC.HasValue &&
                       row.TotalFuelMass.HasValue &&
                       row.AverageSpeed.HasValue &&
                       row.TotalDistanceSailed.HasValue;
            }).ToList();
        }   

Is still not working, im again receiving a list with nullable items.

Thanks in advance

1 Answers

Try making all properties nullable.

Like this:

// Vessel
public int? IMO { get; set; }
public int? MMSI { get; set; }
public int? EEOI { get; set; }
public int? FOC { get; set; }

Edit regarding comment:

Enumerable.Range(0, 10).Select(i => new DataGridInstallationRow()
            {
                Name = $"Vessel {i}",
                IMO = 231,
                MMSI = 1344,
                EEOI = 8121,
                FOC = 123,
                TotalFuelMass = 6817,
                AverageSpeed = 14,
                TotalDistanceSailed = 1560
            }).Where(row =>
            {
                return row.IMO.HasValue && 
                       row.MMSI.HasValue && 
                       row.EEOI.HasValue && 
                       row.FOC.HasValue && 
                       row.TotalFuelMass.HasValue && 
                       row.AverageSpeed.HasValue && 
                       row.TotalDistanceSailed.HasValue;
            }).ToList();
Related