根据时间段检索和显示数据

时间:2014-03-25 16:45:40

标签: asp.net vb.net sql-server-2008

以下代码检索所有活动在用户选择的同一天开始和结束,我需要检索用户在激活期间选择的日期的所有活动

例如,

  活动于3月25日开始,也将于3月25日结束,代码将检索但我需要的是检索25之前开始的活动,25之后结束或25之前开始并25结束活动25活动开始25之后

或者你可以看到我的PIC我的代码正在做但我想做A,B,C和D http://im46.gulfup.com/4Cudk8.jpg

 Public Shared Function SelectByDate(ActivityDate As Date) As List(Of vActivity)

        Dim st As Date = ActivityDate
        Dim et As Date = st.AddDays(1).AddSeconds(-1)

       Using ctx As New ActivitiesEntities
            Dim Lst = (From actv In ctx.vActivities
                       Select actv
                       Where actv.StartTime >= st And actv.EndTime <= et
                       Order By actv.StartTime)

            Return Lst.ToList
        End Using
    End Function

2 个答案:

答案 0 :(得分:0)

Where actv.StartTime <= usertime And actv.EndTime >= usertime

你的问题有点令人困惑,但这似乎就是你所描述的。

您的屏幕截图已被阻止,但是:如果您想要A或B或C ..只需编写您的where子句。

where (actv.StartTime <= usertime And actv.EndTime >= usertime) or (something else) or (something else)

答案 1 :(得分:0)

Dim LstA = (From actv In ctx.vActivities
                       Select actv
                       Where actv.StartTime >= st And actv.EndTime <= et
                       Order By actv.StartTime)

Dim LstB = (From actv In ctx.vActivities
                       Select actv
                       Where actv.StartTime < st And actv.EndTime < st
                       Order By actv.StartTime)

Dim LstC = (From actv In ctx.vActivities
                       Select actv
                       Where actv.StartTime > et And actv.EndTime > et
                       Order By actv.StartTime)

Dim LstD = (From actv In ctx.vActivities
                       Select actv
                       Order By actv.StartTime)