ASP.NET EntityDataSource WHERE 子句

发布于 2024-12-29 05:01:04 字数 2159 浏览 1 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七颜 2025-01-05 05:01:04

您必须在整个数据源声明中使用Where 参数。您可以查看此链接,其中包含了解这些内容的基本教程。 实体数据源过滤

这样

<asp:EntityDataSource ID="RidesEDS" runat="server" 
    ContextTypeName="RamRideOps.RamRideOpsEntities" EnableFlattening="False" 
    EntitySetName="Rides" EnableDelete="True" EnableUpdate="True">

// this needs to be added 
<WhereParameters> 
        <asp:ControlParameter ControlID="yourHiddenFiledID" DbType="YourHiddenFieldDataType" 
          DefaultValue="SomeDefaultValue" Name="NameToDescribe" PropertyName="Text" />
      </WhereParameters>

</asp:EntityDataSource>

如果你想添加程序,那么你可以这样做

RidesEDS.WhereParameters.Add("CategoryID", TypeCode.String, hiddenField.Value);

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

<asp:EntityDataSource ID="RidesEDS" runat="server" 
    ContextTypeName="RamRideOps.RamRideOpsEntities" EnableFlattening="False" 
    EntitySetName="Rides" EnableDelete="True" EnableUpdate="True">

// this needs to be added 
<WhereParameters> 
        <asp:ControlParameter ControlID="yourHiddenFiledID" DbType="YourHiddenFieldDataType" 
          DefaultValue="SomeDefaultValue" Name="NameToDescribe" PropertyName="Text" />
      </WhereParameters>

</asp:EntityDataSource>

if you want to add programatticaly, then you can do like this

RidesEDS.WhereParameters.Add("CategoryID", TypeCode.String, hiddenField.Value);
执着的年纪 2025-01-05 05:01:04

只要看看 Ravi 发布的答案,对我有用的是在代码隐藏中执行此操作,如下所示:

RidesEDS.WhereParameters.Add("CategoryID", TypeCode.String, hiddenField.Value);

然后在 EntityDataSource 的服务器标记中设置它:

AutoGenerateWhereClause="true"

我唯一要添加的是,如果您在代码隐藏中执行此操作,请执行以下操作:确保在添加参数之前进行 (!PostBack) 检查。我在回发时这样做并开始得到疯狂的结果。

Just looking at the answer that Ravi posted, what worked for me was doing it in code behind like this:

RidesEDS.WhereParameters.Add("CategoryID", TypeCode.String, hiddenField.Value);

And then setting this in the server tag of the EntityDataSource:

AutoGenerateWhereClause="true"

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文