Can .NET convert “Yes” & “No” to boolean without If?

后端 未结 12 633
情话喂你
情话喂你 2020-12-17 14:15

You would think there would be a way using DirectCast, TryCast, CType etc but all of them seem to choke on it e.g.:

CType(\"Yes\", Boolean)

12条回答
  •  隐瞒了意图╮
    2020-12-17 14:59

    I like the answer that @thelost posted, but I'm reading values from an ADO.Net DataTable and the casing of the string in the DataTable can vary.

    The value I need to get as a boolean is in a DataTable named childAccounts in a column named Trades.

    I implemented a solution like this:

    bool tradeFlag = childAccounts["Trade"].ToString().Equals("yes", StringComparison.InvariantCultureIgnoreCase);
    

提交回复
热议问题