我在VB ASP.Net项目中使用linq来填充搜索结果。使用intersect来比较查询的单词列表和字符串。我将字符串拆分到空格上以确保两个集合正在交叉。但VB没有意识到这一点并抛出以下错误。
An exception of type 'System.MissingMemberException' occurred in Microsoft.VisualBasic.dll but was not handled in user code
Additional information: Public member 'Count' on type 'String' not found.
select语句中出现的代码如下
MatchCount = keywords.Intersect(IIf(IsNothing(t.TripName), t.tbl_Subject.SubjectName & " in " & t.tbl_City.CityName, t.TripName).ToLower().Split(" ")).Count() / IIf(IsNothing(t.TripName), t.tbl_Subject.SubjectName & " in " & t.tbl_City.CityName, t.TripName).Count
为难以阅读的性质道歉,它在一个select语句中因此必须是一个查询。分成多行,它看起来像这样:
Dim name as string = IIf(IsNothing(t.TripName), t.tbl_Subject.SubjectName & " in " & t.tbl_City.CityName, t.TripName)
MatchCount = keywords.Intersect(name.ToLower().Split(" ")).Count()
编辑: - 整个,原创(不是我写的)选择陈述
toReturn.AddRange(results.Select(Function(t) New SearchResult() _
With {
.MatchCount = keywords.Intersect(If(IsNothing(t.TripName), t.tbl_Subject.SubjectName & " in " & t.tbl_City.CityName, t.TripName).ToLower().Split(" ")).Count() / If(IsNothing(t.TripName), t.tbl_Subject.SubjectName & " in " & t.tbl_City.CityName, t.TripName).Count, _
.UID = t.TripID, _
.Title = IIf(t.TripName Is Nothing, t.tbl_Subject.SubjectName & " in " & t.tbl_City.CityName, t.TripName), _
.Description = t.DescriptionLong.ToString().Replace("<p>", "").Replace("</p>", "").Replace("<b>", "").Replace("</b>", "").Replace("<strong>", "").Replace("</strong>", "").Replace("<i>", "").Replace("</i>", "").Replace("<em>", "").Replace("</em>", "").Replace("<br>", "").Replace("<br />", ""), _
.ImageURL = IIf(t.ImageSub04 Is Nothing, t.tbl_City.ImageThumb, t.ImageSub04),
.URL = "/" & t.tbl_Subject.SubjectWebName & "/" & t.tbl_Country.CountryWebName & "/" & _
IIf(t.TripWebName Is Nothing, _
t.tbl_City.CityWebName, _
t.TripWebName) _
& "_trip/",
.IsCity = keywords.Contains(t.tbl_City.CityName) Or keywords.Contains(t.tbl_Country.CountryName)
}).OrderBy(Function(sr) sr.IsCity).ThenByDescending(Function(sr) sr.MatchCount))
答案 0 :(得分:0)
由于您还在字符串上使用Count()
,为什么不在那里使用Length
?
因为,正如我的问题所说,它在一个选择陈述中,所以我不能 存储变量
您甚至可以在查询中,例如使用匿名类型或(在查询语法中)使用Let
关键字。
在这种情况下,我猜你正在尝试这样做,看看它的可读性有多大:
Dim sResults = From res In results
Let longTripName = String.Format("{0} in {1}", res.tbl_Subject.SubjectName, res.TripName)
Let trip = If(res.TripName, longTripName)
Let tripWords = trip.ToLower().Split(" "c)
Let matching = keywords.Intersect(tripWords).Count()
Let matchCount = matching / trip.Length
Let title = " this is your task "
Let description = " this is your task "
Let imageURL = " this is your task "
Let url = " this is your task "
Let isCity = True ' also your task '
Select sr = New SearchResult With {
.MatchCount = matchCount,
.Title = title,
.Description = description,
.ImageURL = imageURL,
.URL = url,
.IsCity = isCity
}
Order By sr.IsCity, sr.MatchCount Descending
toReturn.AddRange(sResults)
但总的来说,我建议在SearchResult
中实现一个适当的方法或构造函数,它封装了逻辑和复杂性。
此外,由于您正在使用Split(" ")
进行编译,因此您应该真正更改为Option Strict On
。我想你会得到很多编译器错误然后这是一件好事,因为它会向你展示你必须解决的问题。在这里,您必须使用Split(" "c)
。