SORU
11 AĞUSTOS 2011, PERŞEMBE


ConcurrentDictionary AddOrUpdate

Yeniden yazma kodu Sözlük ConcurrentDictionary kullanmaya çalışıyorum. Bazı örnekleri inceledim ama hala sorun AddOrUpdate işlevi uygulamak yaşıyorum. Bu orijinal kodu:

    dynamic a = HttpContext;
    Dictionary<int, string> userDic = this.HttpContext.Application["UserSessionList"] as Dictionary<int, String>;

   if (userDic != null)
   {
      if (useDic.ContainsKey(authUser.UserId))
      {
        userDic.Remove(authUser.UserId);
      }
   }
  else
  {
     userDic = new Dictionary<int,string>();
  }
  userDic.Add(authUser.UserId, a.Session.SessionID.ToString());
  this.HttpContext.Application["UserDic"] = userDic;

Güncelleme kısmı için ne ekleyebilirim bilmiyorum:

userDic.AddOrUpdate(authUser.UserId,
                    a.Session.SessionID.ToString(),
                    /*** what to add here? ***/);

Herhangi bir işaretçiler mutluluk duyacağız.

CEVAP
11 AĞUSTOS 2011, PERŞEMBE


Değeri bir güncelleme durumunda sözlükte depolanacak verir Func geçmesi gerekir. Bu durumda ekleme ve güncelleme arasındaki farkı bilmiyorsun beri) sanırım:

var sessionId = a.Session.SessionID.ToString();
userDic.AddOrUpdate(
  authUser.UserId,
  sessionId,
  (key, oldValue) => sessionId);

I. e. Func her zaman aynı değeri Ekleme Güncelleme ayarlayın, böylece sessionıd verir.

BTW: the MSDN page bir örnek var.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • guau . .

    guau . .

    25 Ocak 2008
  • undrmyumbrellaa

    undrmyumbrel

    25 Temmuz 2012
  • Vagrant Records

    Vagrant Reco

    8 Mayıs 2006