【发布时间】:2015-05-14 10:36:47
【问题描述】:
我在我的 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
为它难以阅读的性质道歉,它在一个选择语句中,所以必须是一个查询。跨多行拆分它看起来像这样:
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))
【问题讨论】:
-
字符串具有
Length,而不是Count属性。无论出于何种原因,keywords.Intersect的结果都是一个字符串。keywords的类型是什么,Intersect的定义是什么?您使用的是Enumerable.Intersect还是您自己的版本? -
为什么不用多行多变量来存储值呢?那将更具可读性。也许您可以在没有
IIFs 的情况下将您的代码抽象为相关问题(您知道有一个If-operator 吗?)。 -
Enumerable.Intersect,关键字是字符串列表。所以返回的类型应该总是可枚举的
-
切换 Option Strict On 并将
IIf更改为If开始。修复在那里抛出的任何错误可能会解决您的整体问题 -
@TimSchmelter +1 - 刚刚意识到那里隐藏着另一个对
Count()的调用
标签: c# asp.net .net vb.net linq