2

My code in my Data Layer DtbseDropDown = ii.DtbseDropDown is throwing an error and I am not sure how to get around it. The error says: "Cannot implicitly convert type 'bool?' to 'bool'. An explicit conversion exists (are you missing a cast?)"

Here are the code blocks for my Data Layer and my Entities

Data Layer

public static List<ListProjectTypes> GetListProjectTypes()

    {
        using (var context = new Sys.EntityModels.HousingRehabEntities())
        {
            // build and execute query against the db
            //return context.ListProjectTypes.Where(x => x.DtbseDropDown=true).ToList();

            // build and execute query against the db
            return (from ii in context.ListProjectTypes
                    where (ii.DtbseDropDown == true)
                    //&& ((ii.LastName + ii.FirstName + ii.Middle) != null))
                    ////&& ((ii.LastName) != null))
                    orderby ii.SortOrder
                    select new Sys.Entities.ListProjectTypes
                    {
                        ProjectType = ii.ProjectType,
                        SortOrder = ii.SortOrder,
                        DtbseDropDown = ii.DtbseDropDown


        }).ToList();

        }
    }
}

Entities

namespace CityOfMesa.HousingRehab.Sys.Entities
{

 public class ListProjectTypes
    {
        public string ProjectType { get; set; }
        public int? SortOrder { get; set; }
        public bool DtbseDropDown { get; set; }
    public ListProjectTypes()
    {

        ProjectType = string.Empty;
        SortOrder = 0;
        DtbseDropDown = true;

    }
}

}

Rahul
  • 76,197
  • 13
  • 71
  • 125
CodeFisher
  • 21
  • 1
  • 3
  • 2
    Possible duplicate of [Convert nullable bool? to bool](http://stackoverflow.com/questions/6075726/convert-nullable-bool-to-bool) – Matt Rowland Jul 21 '16 at 23:00

4 Answers4

2

Yes that's cause your datamodel ii.DtbseDropDown is a nullable bool and thus the error. You should try change it to

public class ListProjectTypes
    {
        public string ProjectType { get; set; }
        public int? SortOrder { get; set; }
        public bool? DtbseDropDown { get; set; }
Rahul
  • 76,197
  • 13
  • 71
  • 125
2

DtbseDropDown property is bool (can have true or false values) whereas ii.DtbseDropDown is bool? (shorthand for Nullable<bool>, i.e. can also be null. See Nullable Types (C# Programming Guide) for more). You're trying to assign a bool? to a bool, hence the error you are getting. What you need to do is check if the bool? struct actually has a value first. If it does (.HasValue), return the actual value (.Value), else return a default value (I've set the default value to false here):

DtbseDropDown = ii.DtbseDropDown.HasValue ? ii.DtbseDropDown.Value : false

You can also use ii.DtbseDropDown.GetValueOrDefault(), as suggested by @test in the comments. The difference between the two is that with my approach you can control what value to output when ii.DtbseDropDown is null, whereas Nullable<T>.GetValueOrDefault returns default(bool) (i.e. false) by default.

Community
  • 1
  • 1
trashr0x
  • 6,457
  • 2
  • 29
  • 39
1

You can use null-coalescing operator.

bool? myBool = null;
bool newBool = myBool ?? false;
Saif
  • 394
  • 3
  • 13
0

Change DtbseDropDown = ii.DtbseDropDown to DtbseDropDown = (bool)ii.DtbseDropDown

kimbaudi
  • 13,655
  • 9
  • 62
  • 74