2011-03-17 19 views
2

我有一個使用LinqDataSource我想使用如下類型的枚舉:ASP.NET LINQ的數據源 - 使用枚舉在參數

<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是我的輸入類型。我似乎無法將一個轉換爲另一個...

+0

經過進一步調查後,我發現我可以修改Where子句從「 Type == @Type「改爲」Type == Guid(@Type)「或」Type == datetime(@Type)「。錯誤因此分別從「字符串」變爲「Guid」和「datetime」。顯然,這不是我想要實現的,但我正在某處...... – EdmundYeung99 2011-03-17 05:17:50

回答

2

我找到了答案 - where子句

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

這實際上是一個類似的問題在什麼可以和不可以在linq where子句中完成,並且與linq轉換爲sql有關。我們不能直接將枚舉轉換爲字符串或字符串來枚舉,所以將它們都轉換爲int。這裏的困難部分是意識到你需要在linq where子句中使用Int32(Type) 並且(int)Type不起作用

1

你的enum loolk是什麼樣的?嘗試添加

[StringValue("stringValue")]裝飾到每個枚舉值

public enum ProductType 
{ 
    [StringValue("1")] 
    Type1 = 1, 
    [StringValue("2")] 
    Type2 = 2, 
    [StringValue("3")] 
    Type3 = 3 
} 
+0

我的枚舉看起來和你的例子很相似。但我認爲錯誤發生在'linq to sql翻譯',它不能做一個直的枚舉==字符串...我需要將字符串轉換爲枚舉或枚舉字符串以某種方式 – EdmundYeung99 2011-03-17 05:21:01