问题描述
我正在打电话给Yelp,以在我写的公寓查找器中获得某些地址的评论,但看起来某些Yelp数据尚未完成,因此以下代码在Ye Olde ORNSTAIOAN中产生错误:
I'm making a call to Yelp, to get reviews for some addresses in an apartment finder I've written, but it looks like some of the Yelp data isn't complete, so the following code results in Ye Olde ORNSTAIOAN error:
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);
}
即使我像上面一样尝试空捕获,错误也会在企业"的返回行中出现.错误输出为:
The error hits in the return line, on "businesses", even if I try a null catch, as I am above. The error outputs as:
<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>
如何捕获缺少的业务信息,并继续/不进行处理?
How can I trap the missing business information, and proceed with/without it?
更新:
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的方法:
Adding in the method for Review 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,
};
}
推荐答案
问题是,在 SelectMany
调用中,您最终返回的是 null
值而不是空值收集到.如果您不希望添加任何值,则必须返回一个空集合,否则 SelectMany
将使ref
The problem is that in the SelectMany
call you end up returning a null
value instead of an empty collection to. In the case you want no values added you must return an empty collection else SelectMany
will null ref
尝试以下
return tasks
.SelectMany(t => t.Result.businesses ?? new List<Business>())
.Select(MapYelpBusinessToReview);
这篇关于未将对象引用设置为对象的实例:Yelp返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!