2016-05-22 7 views
0

データモデルとそれらを移入することは、このようなものです:ネストされた辞書を使用してLINQクエリ

public class UserInfo 
    { 
     public string UserName { get; set; } 
     public int GameId { get; set; } 
     public int Score { get; set; } 
    } 

    public class Game 
    { 
     public int Id { get; set; } 
     public int TypeId { get; set; } 
    } 

    public class Type 
    { 
     public int Id { get; set; } 
     //and many other things 
    } 

は私がしたいことは、そのゲームにおけるユーザーのランクを示し、すべてのゲーム内のすべてのユーザの順番を持つことです。私は毎日更新されます。このため、データをキャッシュしたい

、私がこれまで行ってきたことはこれです:

class Class1 
{ 
    private readonly IDictionary<int, IDictionary<int, IDictionary<string, int>>> _scoreDictionary = 
    new ConcurrentDictionary<int, IDictionary<int, IDictionary<string, int>>>(); 
    private readonly object _scoreDictionaryLock = new object(); 

    private void InitializeDctionary(int typeId, IList<Game> gameInformations, IList<UserInfo> userInformations) 
    { 
     if (!_scoreDictionary.ContainsKey(typeId)) 
     { 
      lock (_scoreDictionaryLock) 
      { 
       foreach (var gameInformation in gameInformations) 
       { 
        var orderdUsers = userInformations.Where(s => s.GameId ==gameInformation.Id).OrderByDescending(s => s.Score).Select((value, index) => new 
       { 
        value, 
        index//this is the user rank 

       }).Select(p => new 
       { 
        p.value.UserName, 
        p.index 
       }).Select(p=>new KeyValuePair<string,int>(p.UserName,p.index)); 
       } 

     // I don't know what to do I want a dictionary that in first level key be typeId and then gameId then userName the final value is user rank. 
        _scoreDictionary.Add(typeId,???????????); 
       } 
      } 
     } 
} 

答えて

1

未テストコードが、あなたのために働く必要があります。

private void InitializeDctionary(int typeId, IList <Game> gameInformations, IList <UserInfo> userInformations) 
{ 
    if (!_scoreDictionary.ContainsKey(typeId)) 
    { 
     lock(_scoreDictionaryLock) 
     { 

      if (!_scoreDictionary.ContainsKey(typeId)) 
      { 
       _scoreDictionary.Add(typeId, new Dictionary < int, Dictionary < string, int >>)); 
      } 

      foreach(var gameInformation in gameInformations) 
      { 
       var orderdUsers = userInformations.Where(s => s.GameId == gameInformation.Id) 
        .OrderByDescending(s => s.Score) 
        .Select((value, index) => 
         new { value, index //this is the user rank 
         }) 
        .Select(p =>new {p.value.UserName,p.index}) 
        .Select(p => new KeyValuePair < string, int > (p.UserName, p.index)); 

       if (!_scoreDictionary[typeId].ContainsKey(gameInformation.Id)) 
        _scoreDictionary[typeId].Add(gameInformation.Id, new Dictionary < string, int >()); 

       _scoreDictionary[typeId][gameInformation.Id] = orderdUsers.ToDictionary(x => x.Key, x => x.Value); 
      } 
     } 
    } 
} 
+0

いくつかの変更を加えると、tnx – someone

関連する問題