Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In the below code there is an error that shown in bold and that getfacetcount is another function in it
VB.NET
Public Function GetFacets(query As Query, facetField As [String]) As IEnumerable(Of KeyValuePair(Of [String], [String]))
    Dim ie As Integer = 0
    If Not facetsCacheContainer.ContainsKey(facetField) Then
        Dim cache As New List(Of FacetValueCache)()

        If reader Is Nothing Then
            Dim directory As Directory = FSDirectory.GetDirectory(MainWindow.folderName)
            Dim analyzer = New KeywordAnalyzer()
            Dim reader = IndexReader.Open(directory, True)
            reader = IndexReader.Open(directory, True)
            Dim parser = New QueryParser(Lucene.Net.Util.Version.LUCENE_29, facetField, analyzer)
        End If

        Dim allDistinctField = FieldCache_Fields.[DEFAULT].GetStrings(reader, facetField).Distinct()

        For Each fieldValue As String In allDistinctField
            Dim facetQuery = New TermQuery(New Term(facetField, fieldValue))
            Dim facetQueryFilter = New CachingWrapperFilter(New QueryWrapperFilter(facetQuery))
            If cache.Exists(Function(cus) cus.FacetValue = fieldValue) Then
                ie = ie + 1
            Else
                cache.Add(New FacetValueCache(fieldValue, facetQueryFilter))

            End If
        Next

        facetsCacheContainer.Add(facetField, cache)
    End If

    'now calculate facets.
    Dim mainQueryFilter = New CachingWrapperFilter(New QueryWrapperFilter(query))
    Dim facetDefinition = facetsCacheContainer(facetField)

'Error here
    Return facetDefinition.Where(Function(fd) fd.GetFacetCount(reader, mainQueryFilter) > 0).[Select](Function(fd) New KeyValuePair(Of [String], [String])(System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(fd.FacetValue) + "("; + fd.GetFacetCount(reader, mainQueryFilter) + ")", fd.FacetValue))
End Function


error is Conversion from string "Reference(" to type 'Double' is not valid.

What I have tried:

i have tried many sites and string but still the error is comes
Posted
Updated 19-Feb-16 2:23am
v2
Comments
Maciej Los 19-Feb-16 5:19am    
WHat line causes the error?
Have you tried to debug your programme?
Member 11835578 19-Feb-16 5:26am    
i have tried it but still error is come and the error is on


return facetDefinition.Where(Function(fd) fd.GetFacetCount(reader, mainQueryFilter) > 0).[Select](Function(fd) New KeyValuePair(Of [String], [String])(System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(fd.FacetValue) + "(" + fd.GetFacetCount(reader, mainQueryFilter) + ")", fd.FacetValue))
E.F. Nijboer 19-Feb-16 5:38am    
It is no surprise that the longest and most unreadable code line is giving you problems and is almost impossible to debug... Split up to smaller code parts using well named variables and functions and you will most likely find the problem before even needing to run it again. Also, the exception message is quite clear. The value "Reference(" clearly cannot be converted to Double.
Member 11835578 19-Feb-16 5:42am    
so any suggestion from you
What can i do ?

It seems odd, but it's not switching fd.GetFacetCount(reader, mainQueryFilter) to a string. It's staying as a double so the '+' then switches from concatenation to addition.

Just add a tostring to make it implicit:

VB
return facetDefinition _
    .Where(Function(fd) fd.GetFacetCount(reader, mainQueryFilter) > 0) 
    .Select(Function(fd) New KeyValuePair(Of [String], [String]) 
    ( 
System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(fd.FacetValue)
+ "(" 
+ fd.GetFacetCount(reader, mainQueryFilter).ToString()
+ ")", fd.FacetValue))
 
Share this answer
 
Comments
Maciej Los 19-Feb-16 7:03am    
Wouldn't be better to use String.Concat() method?
Andy Lanng 19-Feb-16 7:22am    
Personally I often use string.format. No idea if that's a thing in VB. There are enough syntax differences I didn't want to suggest a different way of fixing the string. I just wanted to let the OP know what was going wrong in his code ^_^
Richard Deeming 19-Feb-16 8:24am    
Using + to concatenate strings in VB is decidedly odd. It's always better to use the & operator instead. See Solution #2. :)
In VB.NET, you can use either + or & to concatenation strings:
Concatenation Operators in Visual Basic[^]

The + operator is primarily intended for numeric addition. It has a complex set of rules that determine whether to add, concatenate, produce a compiler error, or throw a run-time exception.

The & operator always converts its operands to strings if necessary, and concatenates the strings.
VB.NET
Dim x As String = "40" + 2 ' Result: "42"
Dim y As String = "40" & 2 ' Result: "402"

Therefore, you should always use the & operator to concatenate strings.
VB.NET
Return facetDefinition.Where(Function(fd) fd.GetFacetCount(reader, mainQueryFilter) > 0).[Select](Function(fd) New KeyValuePair(Of [String], [String])(System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(fd.FacetValue) & "(" & fd.GetFacetCount(reader, mainQueryFilter) & ")", fd.FacetValue))
 
Share this answer
 
Comments
Andy Lanng 19-Feb-16 8:26am    
5'ed. It's been soo long since I really knew VB ^_^

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900