2

我正在打电话给 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);
    }

即使我在上面尝试了一个空捕获,错误也会出现在“企业”的返回行中。错误输出为:

<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>

如何捕获丢失的业务信息,并在有/没有它的情况下继续?

更新:

YelpSharp.Data.Business

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; }
    }
}

添加 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,

        };
    }
4

1 回答 1

2

问题是在SelectMany调用中你最终返回一个null值而不是一个空集合。如果您不想添加任何值,则必须返回一个空集合,否则SelectMany将为 null ref

尝试以下

return tasks
  .SelectMany(t => t.Result.businesses ?? new List<Business>())
  .Select(MapYelpBusinessToReview);
于 2014-03-03T17:08:25.163 回答