Skip to main content
 首页 » 编程设计

c#之每个 session 允许的最大请求异常之解决方法

2024年02月23日19zhenyulu

我收到异常“达到每个 session 允许的最大请求数 (30)”等。 我正在尝试在“for 循环”中运行 RavenDB 查询,我想知道如何解决这个问题。代码如下。

  using (var session = store.OpenSession()) 
        {           
            var MovieList = session.Query<Movies>()                                     
                                 .ToList();          
 
            foreach (var movie in MovieList) 
            { 
               int NumByState = session.Query<Theaters>() 
                                .Where(x => x.State == movie.State) 
                                .Count(); 
 
               string MovieName = movie.MovieName; 
             }                      
 
        }                

查询中的记录有 100 条,我需要在 for 循环内运行计数查询,我看不到另一种在循环外运行查询的方法。因为我需要为每个记录运行此查询列表中的项目。感谢您的帮助。

请您参考如下方法:

ZVenue,这是 Multi Maps/Reduce 索引的完美示例:

public class MoviesWithTheatersCount : AbstractMultiMapIndexCreationTask<MoviesWithTheatersCount.ReduceResult> 
{ 
    public class ReduceResult 
    { 
        public string State { get; set; } 
        public string MovieName { get; set; } 
        public int TheaterCount { get; set; } 
    } 
 
    public MoviesWithTheatersCount() 
    { 
        AddMap<Movie>(movies => from movie in movies 
                                select new 
                                           { 
                                               State = movie.State, 
                                               MovieName = movie.Name, 
                                               TheaterCount = 0 
                                           }); 
 
        AddMap<Theater>(theaters => from theater in theaters 
                                    select new 
                                               { 
                                                   State = theater.State, 
                                                   MovieName = (string)null, 
                                                   TheaterCount = 1 
                                               }); 
 
        Reduce = results => from result in results 
                            group result by result.State 
                            into g 
                            select new 
                                       { 
                                           State = g.Key, 
                                           MovieName = g.Select(x => x.MovieName != null).First(), 
                                           TheaterCount = g.Sum(x => x.TheaterCount) 
                                       }; 
    } 
} 

附带说明:您不应使用无限制的结果集进行查询 - 请改用 .Take()。