在 Linq to SQL 中使用 If 语句
我有这个查询:
Dim Query = From E In db.Employee Select E
现在在 Employee 表中有一个名为“isActive”的布尔字段。
如果 IsActive 值等于 True,我希望结果显示 IsActive 等于“Active”,并且 IsActive=false 相同,以在绑定到 gridview 时显示 InActive”
I have this query:
Dim Query = From E In db.Employee Select E
Now inside the Employee table there is a boolean field Called "isActive".
if the IsActive value equals to True i want the result to show that IsActive equals to "Active" and the same for IsActive=false to show InActive when binding to a gridview"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我正确理解你的问题,你想在 gridview 中显示“Active”或“Inactive”,而不是在 IsActive 列中显示“True”或“False”?
如果是这样,我可以看到两种不同的方法。
变体 1:
你可以选择一个新的匿名类型并绑定到 gridview (请原谅我,我不知道 VB 语法,但这就是在 C# 中完成的方式):
然后你只需像往常一样进行数据绑定。
变体 2:
让 gridview 比仅仅绑定到 IsActive 属性更智能一些。即,gridview 会执行 IsActive ? “活动”:“非活动”部分。
例如,您可能有这样的内容:
您可以将其更改为模板字段:
在代码隐藏文件中,您添加一个方法 GetIsActiveText(),该方法根据布尔 true/false 值返回正确的字符串。
If I understand your question correctly, you want to display "Active" or "Inactive" in your gridview rather than "True" or "False" in the IsActive-column?
If so, I can see two different approaches.
Variant 1:
You could select a new anonymous type and bind to the gridview (forgive me, I don't know the VB syntax but this is how it would be done in C#):
And then you just databind as usual.
Variant 2:
You let the gridview be a little smarter than just bind to the IsActive property. I.e., the gridview would do the
IsActive ? "Active" : "Inactive"
part.For instance, you probably have something like this:
You can change this to a template field instead:
In the code-behind file, you add a method GetIsActiveText() that return the correct string based on the boolean true/false value.
我想我明白你想要实现的目标。您需要这样的内容:
请将“ID”字段替换为您希望在查询中显示的任何其他字段,并用逗号分隔。
I think I understand what you are trying to achieve. You need something like this:
Please replace the "ID" field with whatever other fields you may wish to show in the query, separated by commas.
忽略大家推荐的条件? trueBit : falseBit 语法仅适用于 C#。对于 VB,使用三元 If(不是 IIF):
Ignore everyone recommending the condition ? trueBit : falseBit syntax as that is C# only. For VB, use the Ternary If (not IIF):