0

我有这个 LINQ 查询,它返回大量路线的数据和坐标。当坐标数量超过特定值时,我想排除以下路线的坐标。

这是我尝试执行此操作的方法,问题是当我尝试序列化对象时,JSON.net 序列化超时。

    public static object returnFullSelectWithCoordinates(IQueryable<RouteQueryModel> r)
    {
        int totalCoordinateCount = 0;
        foreach (route x in r.Select(c => c.b))
        {
            if (totalCoordinateCount > DataValues.AmountOfCoordinates)
            {
                x.coordinates.Clear();
            }
            else
            {
                totalCoordinateCount += x.coordinates.Count;
                if (totalCoordinateCount > DataValues.AmountOfCoordinates)
                {
                    x.coordinates.Clear();
                }
            }
        }

        return r.Select(b => new
        {
            route_id = b.b.route_id,
            name = b.b.name,
            description = b.b.description,
            distance = b.b.distance,
            distance_to_route = (int)b.distance_to_from_me,
            departure_place = b.b.departure_place,
            arrival_place = b.b.arrival_place,
            owner = b.b.user.username,
            average_rating = b.avg_rating,
            is_favorite = b.is_favorite,
            date = b.b.date,
            attributes = b.b.route_attributes.Select(c => c.route_attribute_types.attribute_name),
            coordinates = b.b.coordinates.Select(c => new coordinateToSend { sequence = c.sequence, lat = c.position.Latitude, lon = c.position.Longitude })
        });
    }
4

1 回答 1

1

正如 Vladimir Frolov 所指出的,您列举了两次结果。这是浪费时间和资源,但最糟糕的是第二次重新开始,第一次迭代的更改丢失。

所以我想把它转过来:首先获取你需要的数据,这样你就可以从缩小 SQL 查询范围的投影中受益。然后更改任何需要更改的内容:

public static object returnFullSelectWithCoordinates(IQueryable<RouteQueryModel> r)
{
    var results = r.Select(b => new
    {
        route_id = b.b.route_id,
        name = b.b.name,
        description = b.b.description,
        distance = b.b.distance,
        distance_to_route = (int)b.distance_to_from_me,
        departure_place = b.b.departure_place,
        arrival_place = b.b.arrival_place,
        owner = b.b.user.username,
        average_rating = b.avg_rating,
        is_favorite = b.is_favorite,
        date = b.b.date,
        attributes = b.b.route_attributes
                     .Select(c => c.route_attribute_types.attribute_name),
        coordinates = b.b.coordinates.Select(c => new coordinateToSend 
                                                  { sequence = c.sequence,
                                                    lat = c.position.Latitude, 
                                                    lon = c.position.Longitude
                                                  })
    }).ToList(); // ToList !!

    int totalCoordinateCount = 0;
    foreach (var x in results)
    {
        if (totalCoordinateCount + x.coordinates.Count
                                 > DataValues.AmountOfCoordinates)
        {
            x.coordinates.Clear();
        }
        else
        {
            totalCoordinateCount += x.coordinates.Count;
        }
    }

    return results;
}
于 2013-04-03T13:07:43.323 回答