将用户详细信息存储在词典中

时间:2013-03-07 13:52:31

标签: c# asp.net linq

用户登录。用户可以属于许多不同的ServiceAreas我试图将用户的详细信息存储在Session中,其中包含用户的所有信息。当foreach尝试添加相同的key时,Session会继续发生错误。这是我的第一个错误。

Q1。我将如何阻止这种情况发生。

Q2。我是否可以将用户详细信息存储在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; 中,无论它们属于4 serviceAreas还是1?

UserClass

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

if item. Something

我需要为Q1做{{1}}吗?

2 个答案:

答案 0 :(得分: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以找出您对用户的要求。 (即他们是多少个区域的成员)

答案 1 :(得分:0)

我猜你的意思是它正在生成错误的服务区?

如果是这种情况,很容易修复,因为你只有4个服务区。

在ENUMERATION中快速定义您的服务区域

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

现在,当用户能够为多个区域提供服务时,您只需添加VALUES,以便为NORTH和EAST服务的用户总数为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