代码之家  ›  专栏  ›  技术社区  ›  James

Linq动态选角问题

  •  0
  • James  · 技术社区  · 17 年前

    我想运行一个查询,以获取logType的不同“logName”项的字符串数组。以下方法效果很好:

    Dim stringArray() As String = (From item In dc.Vw_Logs 
        Where item.LogType = [Passed in logType] 
        Select item.LogName Distinct).ToArray()
    

    Dim q = From item In dc.Vw_Logs Distinct
    If not logType is nothing Then 
        q = q.Where(Function(item) item.LogType = logType)
    End If
    q.Select(Function(item) item.LogName)
    Dim stringArray() As String = q.ToArray()
    

    Value of type '1-dimensional array of Vw_Log' cannot be converted 
    to '1-dimensional array of String'
    

    3 回复  |  直到 17 年前
        1
  •  1
  •   Adam Ruth    17 年前

    Dim q = From item In dc 
            Where (logType Is Nothing Or item.LogType = logType) 
            Select item.LogName Distinct
    
    
    logType Is Nothing Or item.LogType = logType
    

        2
  •  1
  •   Stu    17 年前

    q.Select(Function(item) item.LogName)
    

    不会改变q,它只是丢弃select的结果,这样当你这样做的时候

    Dim stringArray() As String = q.ToArray()
    

    Dim stringArray() As String = q.Select(Function(item) item.LogName).ToArray()
    
        3
  •  0
  •   Stu    17 年前

    不要重用q。第一个赋值是Vw_Log(),第二个是String()。