How to convert a 'Y' or 'N' Value to a boolean value using linq?
I have this function of mine which selects all room types from the database, I am converting values from a data table to a generic list to optimize the speed of the system that i am creating my problem is how to covert each of the values stored for the field isActive which has a value from the database of "Active"/"Inactive" which i had to convert to a boolean value in C#.net 4.0 can you guys please help me with this?.
internal static List<RoomType> FetchRoomTypeList()
{
List<RoomType> roomTypes = new List<RoomType>();
SqlCommand commRoomTypeSelector = ConnectionManager.MainConnection.CreateCommand();
commRoomTypeSelector.CommandType = CommandType.StoredProcedure;
commRoomTypeSelector.CommandText = "Rooms.asp_RMS_RoomTypeList_Select";
SqlDataAdapter da = new SqlDataAdapter(commRoomTypeSelector);
DataSet ds = new DataSet();
da.Fill(ds);
roomTypes = (from rt in ds.Tables[0].AsEnumerable()
select new RoomType
{
RoomTypeID = rt.Field<int>("RoomTypeID"),
RoomTypeName = rt.Field<string>("RoomType"),
LastEditDate = rt.Field<DateTime>("LastEditDate"),
LastEditUser = rt.Field<string>("LastEditUser"),
IsActive = rt.Field<string>("IsActive") == "Active"
开发者_如何学Python }
).ToList();
return roomTypes;
}
The line below does not return true values, insted it always return false values
IsActive = rt.Field<string>("IsActive") == "Active"
REQUESTED SAMPLE DATA:
Perhaps there are spaces in the field? Try rt.Field<string>("IsActive").Trim()
.
Ok, if you check rt.Field<string>("IsActive")
and it is definitely a single character with a UTF8 value of 89 ('Y'
) or 121 ('y'
) then my next guess is that it's the property.
Is RoomType.IsActive
an automatic property? If not, can you make sure the getter and setter are correct?
And if that's not it, and at return roomTypes;
everything looks ok, then your values are being changed somewhere up the call stack.
Try this.
IsActive = rt.Field<string>("IsActive").Trim().ToUpper().Equals("Y")
There is no need to specify true false.
精彩评论