如果我有日期,如何根据年份过滤实体数据源

时间:2015-08-07 16:56:04

标签: c# sql asp.net sql-server entity-framework

我有一个实体,其中包含以下列:

  1. ID
  2. 名称
  3. 日期
  4. 我需要在实体数据源中放置where条件,例如:

              year(it.Date)=@year   -- is not working !!!
    

    我尝试了选择:

               Select year(it.Date) as xYear... 
    

    然后在我放的地方:

               it.xYear = @year   --- is not working !!!
    

    有人可以给我一个解决方案吗?

    我想在EntityDatasource - Property Window中插入where语句。

    修改

    我是通过使用自定义查询完成的。

          <asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=MyEmmoAppEntities" DefaultContainerName="MyEmmoAppEntities" EnableFlattening="False" 
    Where="" CommandText="select kpivalues .idkpi,kpivalues .ivalue,kpivalues .[date] from kpivalues where year(kpivalues .[date])=2015 and month(kpivalues .[date])=2">
                                </asp:EntityDataSource>
    

2 个答案:

答案 0 :(得分:0)

请尝试使用以下代码:

int year=2015;
SqlDataSource1.SelectCommand = "SELECT * FROM table it WHERE year(it.Date)=@year";
SqlDataSourceArticole.Parameters.Add("@year", System.Data.DbType.Int32, year);

您可以在aspx-File

中指定WhereParameter
<asp:EntityDataSource ID="SearchEntityDataSource" runat="server" 
    .....        
    Where="year(it.Date) = @year" >
    <WhereParameters>
        <asp:ControlParameter ControlID="SearchYearTextBox" Name="year" PropertyName="Text" 
         Type="Int32" DefaultValue="2015"/>
    </WhereParameters>
</asp:EntityDataSource>
<asp:TextBox ID="SearchYearTextBox" runat="server"></asp:TextBox>

this文章中,您可以找到更多详情

答案 1 :(得分:0)

试试这个

it.TableName.Where(x=>x.date==@year);