如何将“Y”转换为“Y”或“N”使用 linq 将值转换为布尔值?
我有一个从数据库中选择所有房间类型的功能,我将数据表中的值转换为通用列表,以优化我正在创建的系统的速度,我的问题是如何隐藏存储的每个值isActive 字段的值来自数据库“Active”/“Inactive”,我必须将其转换为 C#.net 4.0 中的布尔值,你们能帮我解决这个问题吗?
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"
}
).ToList();
return roomTypes;
}
下面的行不返回真值,它总是返回假值
IsActive = rt.Field<string>("IsActive") == "Active"
请求的示例数据:
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"
}
).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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许该字段中有空格?尝试
rt.Field("IsActive").Trim()
。好的,如果您检查
rt.Field("IsActive")
并且它肯定是 UTF8 值为 89 ('Y'
) 或 121 的单个字符('y'
) 那么我的下一个猜测是它是属性。RoomType.IsActive
是自动属性吗?如果不是,你能确保 getter 和 setter 是正确的吗?如果不是这样,并且在
return roomTypes;
时一切看起来都正常,那么您的值将在调用堆栈的某个位置发生更改。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.试试这个。
无需指定 true false。
Try this.
There is no need to specify true false.