簡體   English   中英

ASP.NET EntityDataSource WHERE子句

[英]ASP.NET EntityDataSource WHERE Clause

我是ASP.NET的新手,可以使用一些幫助為EntityDataSource編寫where子句。

我有以下EDS:

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

有一個“旅程”數據庫和一個“ AdminOptions”數據庫,其中包含兩個日期:validDate1和validDate2 ...我需要EDS僅顯示兩個有效日期之間具有“ CallTime”的游樂設施。 為方便起見,在page_load上,我用有效日期(hf_validDate1和hf_validDate2)填充了兩個隱藏字段。 誰能告訴我通過將CallTimes與具有WHERE子句的hf的值進行比較來完成此操作所需添加到EntityDataSource代碼中的內容嗎?

編輯:

到目前為止,這是我目前所沒有的。

<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();
                }
            }            
        }

您必須在整個數據源聲明中使用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);

僅查看Ravi發布的答案,對我有用的就是在背后的代碼中執行以下操作:

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

然后在EntityDataSource的服務器標簽中進行設置:

AutoGenerateWhereClause="true"

我唯一要添加的是如果您在代碼隱藏中執行此操作,請確保在添加參數之前進行了(!PostBack)檢查。 我在回發時就這樣做了,並開始獲得瘋狂的結果。

暫無
暫無

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

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