有没有办法实现' LIKE'在使用LINQ / .NET的日期?我尝试使用自动完成框,允许用户查找时间。
Function GetTimesAvailable(term As String) As ActionResult
Using db As New Context
Dim vals = db.CalendarSlots.Where(Function(c) c.TimeStart.ToString.Contains(term)).Select(Function(c) c.TimeStart).ToList
Return Json(vals, JsonRequestBehavior.AllowGet)
End Using
End Function
此代码不起作用: LINQ to Entities无法识别方法' System.String ToString()'方法,并且此方法无法转换为商店表达式。
答案 0 :(得分:0)
<Extension>
Private Function ContainsSplit(source As String, toCheck As String, comp As StringComparison)
Dim sp = toCheck.Split(" ")
For Each s In sp
If source.IndexOf(s, comp) < 0 Then
Return False
End If
Next
Return True
End Function
<Extension>
Public Function GetTimesAvailable(ByVal Context As Context, term As String) As IEnumerable(Of String)
Using db As New Context
Dim query1 = From c In db.CalendarSlots
Group c By c.TimeStart Into TimeStartGroup = Count()
Select TimeStart
Dim times = query1.ToList
Dim timesAsString = From t In times
Select TimeStart = t.ToLongDateString & " " & t.ToLongTimeString
Dim times2 = timesAsString.ToList
Dim query2 = From t In times2
Where t.ContainsSplit(term, StringComparison.OrdinalIgnoreCase)
Select t
Return query2.ToList
End Using
End Function
我希望在服务器端有一种方式包含。我能想到的唯一方法是使用.toList实现查询,然后执行contains。