gpt4 book ai didi

ASP.NET Linq 数据源 - 在 where 参数中使用枚举

转载 作者:行者123 更新时间:2023-12-04 06:31:41 30 4
gpt4 key购买 nike

我有一个 linqdatasource,我想对 Type 使用枚举,如下所示:

<asp:linqdatasource id="GridDataSource" runat="server" enabledelete="true" Where="Type == @Type">
<whereparameters>
<asp:dynamiccontrolparameter controlid="FilterRepeater" />
<asp:QueryStringParameter QueryStringField="Type" Name="Type" ConvertEmptyStringToNull="false" />
</whereparameters>
</asp:linqdatasource>

它不断抛出错误:
Operator '==' incompatible with operand types 'ProductType' and 'String'

ProductType 是我的 Enum,而 String 是我的输入类型。我似乎无法将一个转换为另一个...

最佳答案

我找到了答案 - 在 where 子句中

Where="Int32(Type) == Convert.ToInt32(@Type)"

这实际上是一个类似的问题,在 linq where 子句中可以做什么和不能做什么,并且与 linq 转换为 sql 有关。我们不能直接将 enum 转换为 string 或 string 到 enum,因此将两者都转换为 int。这里困难的部分是意识到你需要做 Int32(Type)
在 linq where 子句和 (int)Type 中不起作用

关于ASP.NET Linq 数据源 - 在 where 参数中使用枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5334912/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com