2013-03-07 89 views
2

用戶登錄。用戶可以屬於許多不同的ServiceAreas,我試圖嘗試將用戶的詳細信息存儲在包含用戶所有信息的Session中。當它試圖添加相同的key時,foreach不斷髮生錯誤。這是我的第一個錯誤。將用戶詳細信息存儲在字典中

Q1。我將如何阻止這種情況的發生。 Q2302。我能否將用戶詳細信息存儲在Session中,無論它們屬於4服務區域還是1?

var user = (from u in db.tbl_UserServiceAreaDetails 
     where u.tbl_User.UserName.Equals(txt_LoginName.Text) 
     && u.tbl_User.Password.Equals(txt_Password.Text) 
     select u).FirstOrDefault(); 

if (user != null) 
{ 
var serviceAreaID = user.serviceAreaID; 
var serviceArea = user.tbl_ServiceArea.ServiceArea; 

UserClass userDetails = new UserClass(); 

userDetails.UserName = user.tbl_User.UserName; 

foreach (var item in serviceArea) 
{ 
    userDetails.ServiceArea.Add(serviceAreaID, serviceArea);     
} 
Session["User"] = userDetails; 

UserClass

class UserClass 
{ 
    public string UserName { get; set; } 
    public Dictionary<int, string> ServiceArea = new Dictionary<int, string>(); 
} 

我需要做的第一季度if item. Something

+0

什麼是錯誤?密鑰已經存在? – 2013-03-07 13:55:43

+0

是的,它繼續循環循環 – StudentRik 2013-03-07 13:56:20

+0

你爲什麼要迭代serviceArea而不使用迭代值? – 2013-03-07 13:56:35

回答

1

你需要你的鑰匙才能獨一無二!

無法使用服務區ID作爲關鍵,因爲它不是唯一的

「用戶可以屬於多個不同ServiceAreas」

如何在UserClass創建以下:

class UserClass 
{ 
    public string UserName { get; set; } 
    public Dictionary<int, ServiceArea> ServiceAreaDictionary = new Dictionary<int, ServiceArea>(); 
} 

創建一個新的類caled ServiceArea來存儲有關服務區域的信息。

class ServiceArea 
{ 
    public int ServiceAreaID { get; set; } 
    public string ServiceAreaName { get; set; } 

    public ServiceArea(int id, string name) { 
     ServiceAreaID = id; 
     ServiceAreaName = name 
    } 
} 

則...

var user = (from u in db.tbl_UserServiceAreaDetails 
     where u.tbl_User.UserName.Equals(txt_LoginName.Text) 
     && u.tbl_User.Password.Equals(txt_Password.Text) 
     select u).FirstOrDefault(); 

     if (user != null) 
     { 
     var serviceAreaID = user.serviceAreaID; 
     var serviceArea = user.tbl_ServiceArea.ServiceArea; 

     UserClass userDetails = new UserClass(); 

     userDetails.UserName = user.tbl_User.UserName; 

     foreach (var item in serviceArea) 
     { 
      userDetails.ServiceAreaDictionary.Add([SOME UNIQUE KEY HERE], new ServiceArea(serviceAreaID, serviceArea));     
     } 
     Session["User"] = userDetails; 

我希望這將解決您的問題或給你一個想法下一步去哪裏。

此方法應該允許您檢查ServiceAreaDictionary以找出您想要的用戶。 (即他們是多少區域的成員等)

0

我想你的意思是它的服務區的錯誤正在生成?

如果是這種情況,它很容易修復,因爲你只有4個服務區。

在一個枚舉Firtsly定義服務區現在

Public Enum ServiceAreasEnum As Integer 
    AreaN = 1 ' north 
    AreaE = 2 ' East 
    AreaS = 4 ' South 
    AreaW = 8 ' West 
End Enum 

當用戶能夠服務一個以上的區域,您只需添加值,以便服務於華北和華東地區用戶將共3和使用該規則您可以填寫其他缺失的ENUM值

Public Enum ServiceAreasEnum As Integer 
    AreaN = 1 ' NORTH 
    AreaE = 2 ' EAST 
    AreaNE = 3 ' North East 
    AreaS = 4 ' SOUTH 
    AreaNS = 5 ' North South 
    AreaSE = 6 ' South East 
    AreaNES = 7 ' North East South 
    AreaW = 8 ' WEST 
    AreaNW = 9 ' North West 
    AreaEW = 10 ' East West 
    AreaNEW = 11 ' North East West 
    AreaW = 12 ' South West 
    AreaNSW = 13 ' North South West 
    AreaNSE = 14 ' North South East 
    ALLAreas = 15 ' North South West East 
End Enum