ASP.NET EntityDataSource WHERE 子句
我是 ASP.NET 新手,需要一些帮助来为我的 EntityDataSource 编写 where 子句。
我有以下 EDS:
<asp:EntityDataSource ID="RidesEDS" runat="server"
ContextTypeName="RamRideOps.RamRideOpsEntities" EnableFlattening="False"
EntitySetName="Rides" EnableDelete="True" EnableUpdate="True">
</asp:EntityDataSource>
有一个“Rides”数据库和一个“AdminOptions”数据库,其中包含两个日期:validDate1 和 validDate2...我需要 EDS 仅显示在两个有效日期之间具有“CallTime”的游乐设施。为了促进这一点,在 page_load 上,我使用有效日期填充两个隐藏字段(hf_validDate1 和 hf_validDate2)。谁能告诉我需要在 EntityDataSource 代码中添加什么,才能通过使用 WHERE 子句将 CallTimes 与 hf 的值进行比较来实现此目的?
编辑:
这是我到目前为止所拥有的,但还没有完全工作。
<asp:EntityDataSource ID="RidesEDS" runat="server"
ContextTypeName="RamRideOps.RamRideOpsEntities" EnableFlattening="False"
EntitySetName="Rides" EnableDelete="True" EnableUpdate="True" Where="it.TimeOfCall > @validDate1Param AND it.TimeOfCall < @validDate2Param">
<WhereParameters>
<asp:ControlParameter ControlID="hf_validDate1" DbType="DateTime"
DefaultValue="1/01/2012 12:00:00 PM" Name="validDate1Param" PropertyName="Value" />
<asp:ControlParameter ControlID="hf_validDate2" DbType="DateTime"
DefaultValue="1/01/2112 12:00:00 PM" Name="validDate2Param" PropertyName="Value" />
</WhereParameters>
</asp:EntityDataSource>
<asp:HiddenField ID="hf_validDate1" runat="server" />
<asp:HiddenField ID="hf_validDate2" runat="server" />
代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
using(RamRideOpsEntities myEntities = new RamRideOpsEntities())
{
var validDates = (from a in myEntities.AdminOptions
select new { a.ValidDate1, a.ValidDate2 }).FirstOrDefault();
if(validDates != null)
{
hf_validDate1.Value = validDates.ValidDate1.ToString();
hf_validDate1.Value = validDates.ValidDate2.ToString();
}
}
}
I am new to ASP.NET and could use some help writing a where clause for my EntityDataSource.
I have the following EDS:
<asp:EntityDataSource ID="RidesEDS" runat="server"
ContextTypeName="RamRideOps.RamRideOpsEntities" EnableFlattening="False"
EntitySetName="Rides" EnableDelete="True" EnableUpdate="True">
</asp:EntityDataSource>
There is a database of 'Rides' and a database of 'AdminOptions' which contains two dates: validDate1 and validDate2... I need the EDS to only show rides that have 'CallTime's between the two valid dates. To facilitate this, on page_load I am populating two hidden fields with the valid dates (hf_validDate1 and hf_validDate2). Can anyone show me what I need to add to the EntityDataSource code to accomplish this by comparing CallTimes to the values of the hf's with a WHERE clause?
EDIT:
Here is what I have so far, not quite working though..
<asp:EntityDataSource ID="RidesEDS" runat="server"
ContextTypeName="RamRideOps.RamRideOpsEntities" EnableFlattening="False"
EntitySetName="Rides" EnableDelete="True" EnableUpdate="True" Where="it.TimeOfCall > @validDate1Param AND it.TimeOfCall < @validDate2Param">
<WhereParameters>
<asp:ControlParameter ControlID="hf_validDate1" DbType="DateTime"
DefaultValue="1/01/2012 12:00:00 PM" Name="validDate1Param" PropertyName="Value" />
<asp:ControlParameter ControlID="hf_validDate2" DbType="DateTime"
DefaultValue="1/01/2112 12:00:00 PM" Name="validDate2Param" PropertyName="Value" />
</WhereParameters>
</asp:EntityDataSource>
<asp:HiddenField ID="hf_validDate1" runat="server" />
<asp:HiddenField ID="hf_validDate2" runat="server" />
Code-Behind:
protected void Page_Load(object sender, EventArgs e)
{
using(RamRideOpsEntities myEntities = new RamRideOpsEntities())
{
var validDates = (from a in myEntities.AdminOptions
select new { a.ValidDate1, a.ValidDate2 }).FirstOrDefault();
if(validDates != null)
{
hf_validDate1.Value = validDates.ValidDate1.ToString();
hf_validDate1.Value = validDates.ValidDate2.ToString();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在整个数据源声明中使用Where 参数。您可以查看此链接,其中包含了解这些内容的基本教程。 实体数据源过滤
这样
如果你想添加程序,那么你可以这样做
you have to use Where Parameters, in the entiry datasource declaration. you can check this link, which has basic tutorials to understand the things. Entity Datasource filtering
like this
if you want to add programatticaly, then you can do like this
只要看看 Ravi 发布的答案,对我有用的是在代码隐藏中执行此操作,如下所示:
然后在 EntityDataSource 的服务器标记中设置它:
我唯一要添加的是,如果您在代码隐藏中执行此操作,请执行以下操作:确保在添加参数之前进行 (!PostBack) 检查。我在回发时这样做并开始得到疯狂的结果。
Just looking at the answer that Ravi posted, what worked for me was doing it in code behind like this:
And then setting this in the server tag of the EntityDataSource:
The only thing I'd add is if you do it in codebehind, make sure you have a (!PostBack) check before you add the parameter. I was doing it on postback and started getting crazy results.