在 Linq to SQL 中使用 If 语句

发布于 2024-12-12 02:21:40 字数 235 浏览 0 评论 0原文

我有这个查询:

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 技术交流群。

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

发布评论

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

评论(4

无力看清 2024-12-19 02:21:40

如果我正确理解你的问题,你想在 gridview 中显示“Active”或“Inactive”,而不是在 IsActive 列中显示“True”或“False”?

如果是这样,我可以看到两种不同的方法。

变体 1:

你可以选择一个新的匿名类型并绑定到 gridview (请原谅我,我不知道 VB 语法,但这就是在 C# 中完成的方式):

Dim Query = From E In db.Employee Select New With _
{ _
    .Id = E.Id, _
    .Name = E.Name, _
    .IsActive = E.IsActive ? "Active" : "Inactive" _
    ' ... all employee fields except IsActive, '
}

然后你只需像往常一样进行数据绑定。

变体 2:

让 gridview 比仅仅绑定到 IsActive 属性更智能一些。即,gridview 会执行 IsActive ? “活动”:“非活动”部分。

例如,您可能有这样的内容:

<asp:BoundField DataField="IsActive" HeaderText="IsActive" />

您可以将其更改为模板字段:

<asp:TemplateField HeaderText="IsActive">
    <ItemTemplate>
        <%# GetIsActiveText((bool)Eval("IsActive")) %>
    </ItemTemplate>
</asp:TemplateField>

在代码隐藏文件中,您添加一个方法 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#):

Dim Query = From E In db.Employee Select New With _
{ _
    .Id = E.Id, _
    .Name = E.Name, _
    .IsActive = E.IsActive ? "Active" : "Inactive" _
    ' ... all employee fields except IsActive, '
}

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:

<asp:BoundField DataField="IsActive" HeaderText="IsActive" />

You can change this to a template field instead:

<asp:TemplateField HeaderText="IsActive">
    <ItemTemplate>
        <%# GetIsActiveText((bool)Eval("IsActive")) %>
    </ItemTemplate>
</asp:TemplateField>

In the code-behind file, you add a method GetIsActiveText() that return the correct string based on the boolean true/false value.

与风相奔跑 2024-12-19 02:21:40
ActiveStr = e.IsActive?"Active":"Inactive" 
ActiveStr = e.IsActive?"Active":"Inactive" 
甜心 2024-12-19 02:21:40

我想我明白你想要实现的目标。您需要这样的内容:

Dim Query = From e In db.Employee 
      select
      new
      {
       ID = e.ID,
       ActiveStr = e.IsActive?"Active":"Inactive"
      };

请将“ID”字段替换为您希望在查询中显示的任何其他字段,并用逗号分隔。

I think I understand what you are trying to achieve. You need something like this:

Dim Query = From e In db.Employee 
      select
      new
      {
       ID = e.ID,
       ActiveStr = e.IsActive?"Active":"Inactive"
      };

Please replace the "ID" field with whatever other fields you may wish to show in the query, separated by commas.

蛮可爱 2024-12-19 02:21:40

忽略大家推荐的条件? trueBit : falseBit 语法仅适用于 C#。对于 VB,使用三元 If(不是 IIF):

Dim query = From e In db.Employee  
            Select e.ID, 
                   ActiveStr = If(e.IsActive,"Active","Inactive")

Ignore everyone recommending the condition ? trueBit : falseBit syntax as that is C# only. For VB, use the Ternary If (not IIF):

Dim query = From e In db.Employee  
            Select e.ID, 
                   ActiveStr = If(e.IsActive,"Active","Inactive")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文