【问题标题】:LINQ function to return list but compiler says function doesn't return a value on all code pathLINQ 函数返回列表,但编译器说函数没有在所有代码路径上返回值
【发布时间】:2013-06-11 18:46:22
【问题描述】:

这是我的代码

<WebMethod()> _
    Public Function getlocationsbypro(searchtype As String, crime As String, proid As String, startdate As String, enddate As String, starttime As String, endtime As String) As List(Of crimelocation)

    If searchtype = "withdatetime" Then
        Dim locs As New crimemsapslocationDataContext
        Dim giveloc = From locations In locs.crimelocations _
                      Where locations.INCIDENTTYPE = crime And (locations.DATE_COMTD >= Convert.ToDateTime(startdate) And locations.DATE_COMTD <= Convert.ToDateTime(enddate)) _
                      And (locations.gettimecom >= starttime And locations.gettimecom <= endtime) _
                      Select locations
        Return giveloc.ToList
    ElseIf searchtype = "withdate" Then
        Dim locs As New crimemsapslocationDataContext
        Dim giveloc = From locations In locs.crimelocations _
                      Where locations.INCIDENTTYPE = crime And (locations.DATE_COMTD >= Convert.ToDateTime(startdate) And locations.DATE_COMTD <= Convert.ToDateTime(enddate)) _
                      Select locations

        Return giveloc.ToList
    ElseIf searchtype = "without" Then
        Dim locs As New crimemsapslocationDataContext
        Dim giveloc = From locations In locs.crimelocations _
                      Where locations.INCIDENTTYPE = crime _
                      Select locations

        Return giveloc.ToList
    End If

End Function

但是当我编译它时它说该函数没有在所有代码路径上返回一个值,尽管我的所有 if 语句都有一个 return 语句我错过了一些东西,而且我注意到如果没有 elseif 语句只是通常的if else end 它不会给我上述错误。

【问题讨论】:

    标签: asp.net vb.net linq list


    【解决方案1】:

    您需要在 End If 之后有另一个 Return 语句。否则,如果不满足 If 或 ElseIf 条件,您的代码将不会返回任何内容。

    【讨论】:

    • 感谢你们的快速回答,是的,我忘记了 evanmcdonnal 说它更好的 else 声明
    【解决方案2】:

    所有代码路径都不返回值。

     If searchtype = "withdatetime" Then
            Dim locs As New crimemsapslocationDataContext
            Dim giveloc = From locations In locs.crimelocations _
                          Where locations.INCIDENTTYPE = crime And (locations.DATE_COMTD >= Convert.ToDateTime(startdate) And locations.DATE_COMTD <= Convert.ToDateTime(enddate)) _
                          And (locations.gettimecom >= starttime And locations.gettimecom <= endtime) _
                          Select locations
            Return giveloc.ToList
        ElseIf searchtype = "withdate" Then
            Dim locs As New crimemsapslocationDataContext
            Dim giveloc = From locations In locs.crimelocations _
                          Where locations.INCIDENTTYPE = crime And (locations.DATE_COMTD >= Convert.ToDateTime(startdate) And locations.DATE_COMTD <= Convert.ToDateTime(enddate)) _
                          Select locations
    
            Return giveloc.ToList
        ElseIf searchtype = "without" Then
            Dim locs As New crimemsapslocationDataContext
            Dim giveloc = From locations In locs.crimelocations _
                          Where locations.INCIDENTTYPE = crime _
                          Select locations
    
            Return giveloc.ToList
        End If
     // if none of the if-else conditions are met your code goes straight to here and there is not return statement
        Return null; // this fixes it.
    

    您也可以在 if-else 末尾添加一个else,这可能会更好。我没有注意到 ElseIf 的出现,因为我不写那些 VB 的东西。

    【讨论】:

    • 是的!我应该在最后一行写一个 else 语句?你是这么说的吗?
    • @user2334366 您可以在其外部放置一个 return 语句(我在代码示例中有它),或者您可以在末尾添加一个 Else 并将其放在那里。无论哪种方式都可以。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多