2

用户登录。用户可以属于许多不同的 ServiceAreas 我试图尝试将用户的详细信息存储在一个Session包含用户所有信息的文件夹中。当foreach它尝试添加相同的key. 这是我的第一个错误。

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

Q2。我能否将用户详细信息存储在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为 Q1 做一个吗?

4

2 回答 2

1

您需要独一无二的钥匙!

您不能使用服务区 ID 作为键,因为它不是唯一的

“用户可以属于许多不同的服务区域”

如何UserClass创建以下内容:

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

创建一个名为 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以找出您想要的用户。(即他们是多少个区域的成员等)

于 2013-03-07T14:03:27.090 回答
0

我猜你的意思是它是产生错误的服务区?

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

首先在枚举中定义您的服务区域

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

现在,当用户能够为多个区域提供服务时,您只需添加值,以便为 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
于 2013-03-07T14:16:49.663 回答