如何将“Y”转换为“Y”或“N”使用 linq 将值转换为布尔值?

发布于 2024-12-08 16:04:34 字数 1591 浏览 3 评论 0原文

我有一个从数据库中选择所有房间类型的功能,我将数据表中的值转换为通用列表,以优化我正在创建的系统的速度,我的问题是如何隐藏存储的每个值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:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾北清歌寒 2024-12-15 16:04:34

也许该字段中有空格?尝试 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.

疾风者 2024-12-15 16:04:34

试试这个。

IsActive = rt.Field<string>("IsActive").Trim().ToUpper().Equals("Y")

无需指定 true false。

Try this.

IsActive = rt.Field<string>("IsActive").Trim().ToUpper().Equals("Y")

There is no need to specify true false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文