我正在打电话给Yelp,在我写的公寓查找器中查看某些地址的评论,但看起来有些Yelp数据不完整,所以下面的代码会导致Ye Olde ORNSTAIOAN错误:
public IEnumerable<Review> GetReviews(Bounds searchBounds) {
var yelp = new Y.Yelp(ConfigOptions);
var searchOptions = GetSearchOptions(searchBounds);
var searchTask = yelp.Search(searchOptions);
var tasks = new Task<Y.Data.SearchResults>[GetTaskArraySize(searchTask)];
tasks[0] = searchTask;
for (var i = 1; i < tasks.Length; i++) {
searchOptions.GeneralOptions.offset = i * YelpResultsLimit;
tasks[i] = yelp.Search(searchOptions);
}
Task.WaitAll(tasks);
return tasks.SelectMany(t => t.Result.businesses != null
? t.Result.businesses
: null)
.Select(MapYelpBusinessToReview);
}
错误在“business”的返回行中命中,即使我尝试null catch,如上所述。错误输出为:
<Error>
<script id="tinyhippos-injected"/>
<Message>
An error has occurred.
</Message>
<ExceptionMessage>
Object reference not set to an instance of an object.
</ExceptionMessage>
<ExceptionType>
System.NullReferenceException
</ExceptionType>
<StackTrace>
at System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext() at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at ApartmentFinder.Infrastructure.Services.SearchEngine.<>c__DisplayClassc.<FindProperties>b__1() in c:\Development\myProject\ApartmentFinder.Infrastructure\Services\SearchEngine.cs:line 25 at System.Threading.Tasks.Task`1.InnerInvoke() at System.Threading.Tasks.Task.Execute()
</StackTrace>
</Error>
如何捕获丢失的商业信息,然后进行/不进行?
更新
namespace ApartmentFinder.Infrastructure.Models {
/// <summary>
/// Provides the set of properties describing a review for a property.
/// </summary>
public class Review {
public string Id{ get; set; }
public string Name{ get; set; }
public string Address{ get; set; }
public string City{ get; set; }
public string State{ get; set; }
public string ZipCode{ get; set; }
public string PhoneNumber{ get; set; }
public string RatingImageUrl{ get; set; }
public int NumberOfReviews{ get; set; }
public string Snippet{ get; set; }
public string Url{ get; set; }
public double Latitude{ get; set; }
public double Longitude{ get; set; }
}
}
添加查看MapYelpBusinessToReview的方法:
/// <summary>
/// Maps a <see cref="Y.Data.Business"/> instance to a new <see cref="Review"/>.
/// </summary>
/// <param name="business">Business object to map.</param>
/// <returns>A new <see cref="Review"/> instance.</returns>
static Review MapYelpBusinessToReview(Y.Data.Business business) {
return new Review {
Id = business.id,
Name = business.name,
Address = business.location.address[0],
City = business.location.city,
State = business.location.state_code,
ZipCode = business.location.postal_code,
PhoneNumber = business.phone,
RatingImageUrl = business.rating_img_url,
Snippet = business.snippet_text,
Url = business.url,
NumberOfReviews = business.review_count,
};
}
答案 0 :(得分:2)
问题是,在SelectMany
调用中,您最终会返回null
值而不是空集合。如果您不想添加任何值,则必须返回空集合,否则SelectMany
将为null ref
尝试以下
return tasks
.SelectMany(t => t.Result.businesses ?? new List<Business>())
.Select(MapYelpBusinessToReview);