views:

514

answers:

3

I want to run a query to get a string array of distinct "logName" items for a logType. The following works great:

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

However, this only works when a specific LogType is set. I would like the logType clause to be optional. To try and achieve this I have rewritten the query:

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()

With this I get the following error:

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

What is the best way to get round this? I would like to avoid iterating each item and casting.

Thanks for any help

A: 

Do not reuse q. The first assignment is Vw_Log(), the second is String().

You can also combine the last two lines to avoid this.

Stu
Sorry - i can't seem to get the above comment to format any better!
James
This seems to result in an inner SQL select statement:Dim qTyped = From item In dc.Vw_Log DistinctIf logType <> Nothing Then qTyped = qTyped.Where(Function(item) item.LogType = logType)Dim q = qTyped.Select(Function(item) item.LogName).Distinct()
James
+1  A: 

The error is because q is not strings but the type contained in dc.Vw_Logs.

I don't do much programming in VB anymore but this should get you close.

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

If logType has a value, you will get the items that match; otherwise, you will get all of the items.

Jeremy
This wont work because if the logType (passed into the select method) is nothing, then this will result in no matches (I want all matches in this case)
James
Jeremy just misread your logic, but he's fundamentally correct. The Where clause should just be (logType Is Nothing Or item.LogType = logType)
Collin K
Thanks for the catch.
Jeremy
I think we have our wires crossed. The logType in the database will ALWAYS have a value. However, the method that I am trying to make takes a string of "logType". If this is null, I would like the linq query to simply not include a filter for the logType (thus getting across all log types). If I use the suggested logic above, this will always return no results when the logType passed in to the method is null.
James
I just checked and it works! I did not know you could mix checks against both server variables and database values in a single linq where statement. Wow! That is great. Thanks for your assistance.
James
+1  A: 

The problem is that this line

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

doesn't change q, it simply discards the result of the select so that when you do

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

You're trying to convert the original q (which is of type Vw_Log) to an array of strings. Replace the last two lines with this:

Dim stringArray() As String = q.Select(Function(item) item.LogName).ToArray()
Adam Ruth