簡體   English   中英

EntityDataSource WHERE IN

[英]EntityDataSource WHERE IN

我有一個asp:EntityDataSource

<asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=MyEntities"  DefaultContainerName="MyEntities" 
    EntitySetName="Student" Where="it.Age = 12 or it.Age = 13"> 
</asp:EntityDataSource>

現在我需要顯示更多的年齡。 有沒有辦法寫得更短?

我的嘗試:

<asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=MyEntities"  DefaultContainerName="MyEntities" 
    EntitySetName="Student" Where="it.Age IN (11,12,13,14)"> 
</asp:EntityDataSource>

但這會引發錯誤

集合表達式的正確參數必須是 CollectionType。

Where EntityDataSource Where屬性上使用IN子句的正確方法是將花括號塊放在所有值(如數組聲明)周圍:

<asp:EntityDataSource ID="EntityDataSource1" runat="server" 
    ConnectionString="name=MyEntities"  DefaultContainerName="MyEntities" 
    EntitySetName="Student" Where="it.Age IN {11,12,13,14}"> 
</asp:EntityDataSource>

使用大括號的原因可能取決於ObjectQuery ,它接收針對給定上下文中的模型的類型查詢,它可能接受值數組。 換句話說,IN 子句在 LINQ 格式上具有等價性,如下所示:

int[] ages = new int[] {11, 12, 13, 14};
var query = MyEntities.Student.Where(it => it.Age.Contains(ages));

生成的 SQL 命令將變為:

SELECT * FROM [MyEntities].[Student] WHERE Age IN (11,12,13,14)

其他可能的原因是 ASPX 頁面標記中的服務器控件語法遵循關於屬性值模板的XSLT 約定,它將花括號塊解釋為字符串值表達式,並將括號視為方法參數/表達式外殼(請參閱XSL 轉換詳細信息)。

類似問題:

VB.NET 中的 EntityDataSource Where 子句

補充參考:

創建 IN 查詢

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM