请高手指点,简单的几个数组操作方法不知道是否可以有更好的改进方法或者更简单的方法?
2009-09-18 13:03 通用C#系统架构 阅读(2645) 评论(79) 编辑 收藏 举报
总觉得以下方法写得不是最优化,希望得到高手指点,如何改进代码量最少,运行速度最优?
还浪费感情,还需要删除掉手机里的没用短信,这就是所谓的细节吧,那就应该是从发送目标数组里把当前
操作员去掉比较友善一些。
例如,我要点选好几个部门,要知道这些部门里都有哪些用户?并且合并数组,得到最终的目标数组
每天提高一点点,每天变得谦虚一点点。
导读:
疯狂.NET架构通用权限后台管理工具演示版2.0下载
疯狂.NET 通用权限设计 C\S后台管理,B\S前台调用源码样例程序源码下载之 --- 操作权限
疯狂.NET 通用权限设计 C\S后台管理,B\S前台调用源码样例程序源码下载之 --- 角色权限
疯狂.NET 通用权限设计 C\S后台管理,B\S前台调用源码样例程序源码下载之 --- 数据集权限
淘宝店地址:
http://shop59297253.taobao.com/
1
/// <summary>
2
/// 判断是否包含的方法
3
/// </summary>
4
/// <param name="ids">数组</param>
5
/// <param name="targetString">目标值</param>
6
/// <returns>包含</returns>
7
public static bool Exists(String[] ids, String targetString)
8
{
9
bool returnValue = false;
10
for (int i = 0; i < ids.Length; i++)
11
{
12
if (ids[i].Equals(targetString))
13
{
14
returnValue = true;
15
break;
16
}
17
}
18
return returnValue;
19
}
20
21
/// <summary>
22
/// 合并数组
23
/// </summary>
24
/// <param name="ids">数组</param>
25
/// <returns>数组</returns>
26
public static String[] Concat(params String[][] ids)
27
{
28
// 进行合并
29
Hashtable hashValues = new Hashtable();
30
if (ids != null)
31
{
32
for (int i = 0; i < ids.Length; i++)
33
{
34
if (ids[i] != null)
35
{
36
for (int j = 0; j < ids[i].Length; j++)
37
{
38
if (!hashValues.ContainsKey(ids[i][j]))
39
{
40
hashValues.Add(ids[i][j], ids[i][j]);
41
}
42
}
43
}
44
}
45
}
46
// 返回合并结果
47
String[] returnValues = new String[hashValues.Count];
48
IDictionaryEnumerator enumerator = hashValues.GetEnumerator();
49
int key = 0;
50
while (enumerator.MoveNext())
51
{
52
returnValues[key] = (String)(enumerator.Key.ToString());
53
key++;
54
}
55
return returnValues;
56
}
57![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
58
/// <summary>
59
/// 从目标数组中去除某个值
60
/// </summary>
61
/// <param name="ids">数组</param>
62
/// <param name="id">目标值</param>
63
/// <returns>数组</returns>
64
public static String[] Remove(String[] ids, String id)
65
{
66
// 进行合并
67
Hashtable hashValues = new Hashtable();
68
if (ids != null)
69
{
70
for (int i = 0; i < ids.Length; i++)
71
{
72
if (ids[i] != null && (!ids[i].Equals(id)))
73
{
74
if (!hashValues.ContainsKey(ids[i]))
75
{
76
hashValues.Add(ids[i], ids[i]);
77
}
78
}
79
}
80
}
81
// 返回合并结果
82
String[] returnValues = new String[hashValues.Count];
83
IDictionaryEnumerator enumerator = hashValues.GetEnumerator();
84
int key = 0;
85
while (enumerator.MoveNext())
86
{
87
returnValues[key] = (String)(enumerator.Key.ToString());
88
key++;
89
}
90
return returnValues;
91
}
日常开发中经常会遇到对数组的处理,例如发送短信提醒时,总没必要给自己也发一条,那不是浪费5分钱,![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
89
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
90
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
91
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
还浪费感情,还需要删除掉手机里的没用短信,这就是所谓的细节吧,那就应该是从发送目标数组里把当前
操作员去掉比较友善一些。
1
/// <summary>
2
/// 邮件提醒,需要发给别人邮件手机短信提醒的功能接口
3
/// </summary>
4
/// <param name="dbHelper">数据库连接</param>
5
/// <param name="userInfo">当前用户</param>
6
/// <param name="userIDs">用户代码组</param>
7
/// <param name="messageContent">短信内容</param>
8
/// <returns>发送短信条数</returns>
9
public static int SendMessage(IDbHelper dbHelper, BaseUserInfo userInfo, String[] userIDs, String messageContent)
10
{
11
// 没必要自己给自己发邮件,把自己从发邮件列表里去掉
12
userIDs = BaseBusinessLogic.Remove(userIDs, userInfo.ID);
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
例如,我要点选好几个部门,要知道这些部门里都有哪些用户?并且合并数组,得到最终的目标数组
1 public String[] GetUserIDs(String organizeID)
2 {
3 // 要注意不能重复发信息,只能发一次。
4 String[] companyUsers = null; // 按公司查找用户
5 String[] departmentUsers = null; // 按部门查找用户
6 String[] workgroupUsers = null; // 按工作组查找用户
7 if (!String.IsNullOrEmpty(organizeID))
8 {
9 // 这里获得的是用户主键,不是员工代码
10 BaseStaffDao staffDao = new BaseStaffDao(this.DbHelper);
11 companyUsers = staffDao.GetIDs(BaseStaffTable.FieldCompanyID, organizeID, BaseStaffTable.FieldUserID);
12 departmentUsers = staffDao.GetIDs(BaseStaffTable.FieldDepartmentID, organizeID, BaseStaffTable.FieldUserID);
13 workgroupUsers = staffDao.GetIDs(BaseStaffTable.FieldWorkgroupID, organizeID, BaseStaffTable.FieldUserID);
14 }
15 String[] userIDs = BaseBusinessLogic.Contact(companyUsers, departmentUsers, workgroupUsers);
16 return userIDs;
17 }
18
19 public String[] GetUserIDs(String[] organizeIDs, String[] roleIDs)
20 {
21 // 要注意不能重复发信息,只能发一次。
22 String[] companyUsers = null; // 按公司查找用户
23 String[] departmentUsers = null; // 按部门查找用户
24 String[] workgroupUsers = null; // 按工作组查找用户
25 if (organizeIDs != null)
26 {
27 // 这里获得的是用户主键,不是员工代码
28 BaseStaffDao staffDao = new BaseStaffDao(this.DbHelper);
29 companyUsers = staffDao.GetIDs(BaseStaffTable.FieldCompanyID, organizeIDs, BaseStaffTable.FieldUserID);
30 departmentUsers = staffDao.GetIDs(BaseStaffTable.FieldDepartmentID, organizeIDs, BaseStaffTable.FieldUserID);
31 workgroupUsers = staffDao.GetIDs(BaseStaffTable.FieldWorkgroupID, organizeIDs, BaseStaffTable.FieldUserID);
32 }
33 String[] roleUsers = null;
34 if (roleIDs != null)
35 {
36 BaseUserRoleDao userRoleDao = new BaseUserRoleDao(this.DbHelper);
37 roleUsers = userRoleDao.GetIDs(BaseUserRoleTable.FieldRoleID, roleIDs, BaseUserRoleTable.FieldUserID);
38 }
39 String[] userIDs = BaseBusinessLogic.Contact(companyUsers, departmentUsers, workgroupUsers, roleUsers);
40 return userIDs;
41 }
42
43 public String[] GetUserIDs(String[] userIDs, String[] organizeIDs, String[] roleIDs)
44 {
45 return BaseBusinessLogic.Contact(userIDs, this.GetUserIDs(organizeIDs, roleIDs));
46 }
2 {
3 // 要注意不能重复发信息,只能发一次。
4 String[] companyUsers = null; // 按公司查找用户
5 String[] departmentUsers = null; // 按部门查找用户
6 String[] workgroupUsers = null; // 按工作组查找用户
7 if (!String.IsNullOrEmpty(organizeID))
8 {
9 // 这里获得的是用户主键,不是员工代码
10 BaseStaffDao staffDao = new BaseStaffDao(this.DbHelper);
11 companyUsers = staffDao.GetIDs(BaseStaffTable.FieldCompanyID, organizeID, BaseStaffTable.FieldUserID);
12 departmentUsers = staffDao.GetIDs(BaseStaffTable.FieldDepartmentID, organizeID, BaseStaffTable.FieldUserID);
13 workgroupUsers = staffDao.GetIDs(BaseStaffTable.FieldWorkgroupID, organizeID, BaseStaffTable.FieldUserID);
14 }
15 String[] userIDs = BaseBusinessLogic.Contact(companyUsers, departmentUsers, workgroupUsers);
16 return userIDs;
17 }
18
19 public String[] GetUserIDs(String[] organizeIDs, String[] roleIDs)
20 {
21 // 要注意不能重复发信息,只能发一次。
22 String[] companyUsers = null; // 按公司查找用户
23 String[] departmentUsers = null; // 按部门查找用户
24 String[] workgroupUsers = null; // 按工作组查找用户
25 if (organizeIDs != null)
26 {
27 // 这里获得的是用户主键,不是员工代码
28 BaseStaffDao staffDao = new BaseStaffDao(this.DbHelper);
29 companyUsers = staffDao.GetIDs(BaseStaffTable.FieldCompanyID, organizeIDs, BaseStaffTable.FieldUserID);
30 departmentUsers = staffDao.GetIDs(BaseStaffTable.FieldDepartmentID, organizeIDs, BaseStaffTable.FieldUserID);
31 workgroupUsers = staffDao.GetIDs(BaseStaffTable.FieldWorkgroupID, organizeIDs, BaseStaffTable.FieldUserID);
32 }
33 String[] roleUsers = null;
34 if (roleIDs != null)
35 {
36 BaseUserRoleDao userRoleDao = new BaseUserRoleDao(this.DbHelper);
37 roleUsers = userRoleDao.GetIDs(BaseUserRoleTable.FieldRoleID, roleIDs, BaseUserRoleTable.FieldUserID);
38 }
39 String[] userIDs = BaseBusinessLogic.Contact(companyUsers, departmentUsers, workgroupUsers, roleUsers);
40 return userIDs;
41 }
42
43 public String[] GetUserIDs(String[] userIDs, String[] organizeIDs, String[] roleIDs)
44 {
45 return BaseBusinessLogic.Contact(userIDs, this.GetUserIDs(organizeIDs, roleIDs));
46 }
每天提高一点点,每天变得谦虚一点点。
导读:
疯狂.NET架构通用权限后台管理工具演示版2.0下载
疯狂.NET 通用权限设计 C\S后台管理,B\S前台调用源码样例程序源码下载之 --- 操作权限
疯狂.NET 通用权限设计 C\S后台管理,B\S前台调用源码样例程序源码下载之 --- 角色权限
疯狂.NET 通用权限设计 C\S后台管理,B\S前台调用源码样例程序源码下载之 --- 数据集权限
淘宝店地址:
http://shop59297253.taobao.com/
将权限管理、工作流管理做到我能力的极致,一个人只能做好那么很少的几件事情。