Toshi
Toshi

Reputation: 2608

EntityDataSource WHERE IN

I have a 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>

now I need to show more ages. Is there a way to write that shorter?

My try:

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

but that throws an error

The right argument of the set expression must be of CollectionType.

Upvotes: 3

Views: 1113

Answers (1)

Tetsuya Yamamoto
Tetsuya Yamamoto

Reputation: 24957

The right way to use IN clause on Where attribute of EntityDataSource is putting curly brace block surrounding all values like array declaration:

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

The reason behind usage of curly braces possibly depends on ObjectQuery which receives typed query against model on given context, which may accepts array of values. In other words, the IN clause have equivalent on LINQ format as this:

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

And generated SQL command would become:

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

Other possible reason is server control syntax in ASPX page markup follows XSLT convention regarding attribute value templates, which interprets curly brace blocks as string-value expression and parentheses treated as method parameter/expression enclosure (see XSL transformation details).

Similar issue:

EntityDataSource Where Clause in VB.NET

Additional reference:

Creating IN Queries

Upvotes: 3

Related Questions