最近工做过程当中遇到一点写程序思路不清晰,简单的问题复杂化的状况;特总结一下几个小知识点但愿再次遇到相似的状况时把程序简单化:
考虑到linq实现程序的多个字段查询判断,考虑用dictionary和hashtable将程序简单化.
1.对于这种自建立的排序虽然不难,可是本身在写代码时每每想不到用这种方式去处理问题,下次再遇到相似的状况时能够从这个思路出发,或许思路会简单不少;
C#中自建立排序并调用:
//对数据进行降序排序
public static int SortDesc(AuditDto audit1,AuditDto audit2)
{
if (audit1.createdDate != null && audit2.createdDate != null)
{
if (audit1.createdDate < audit2.createdDate)
{
return 1;
}
else if (audit1.createdDate == audit2.createdDate)
{
return 0;
}
return -1;
}
return 1;
}
如下为调用的方法:
//公司分管领导审批意见/日
protected AuditDto GetCompanyLeatestAuditDto(List<AuditDto> dtos, string str,string status)
{
List<AuditDto> Alldots = new List<AuditDto>();
foreach (AuditDto dto in dtos)
{
if (dto.udf1 == str && dto.mainStatus == status)
{
Alldots.Add(dto);
}
}
if (Alldots.Count > 0)
{
Alldots.Sort(new Comparison<AuditDto>(SortDesc));
return Alldots[0];
}
return null;
}
如下为自定义处理decimal类型的格式化方法
protected string FormatDecimal(decimal? d)
{
if (d == null) return "";
string str = d.ToString();
if (str.IndexOf(".") == -1)
{
str = str + ".00";
}
else if (str.IndexOf(".") > -1)
{
string[] strArr = str.Split('.');
if (strArr[1].Length == 1)
{
str = str + "0";
}
}
return str;
}
根据条件判断是是否要加入集合:
能够经过linq去判断条件是否成立
List<HRWorkOverTimeDto> rest = new List<HRWorkOverTimeDto>();
for (int i = 0; i < gps.Count; i++)
{
if (rest.Find(x => x.overTimePeople_hrBaseName == gps[i].overTimePeople_hrBaseName && x.overTimeStartDate == gps[i].overTimeStartDate && x.overTimeEndDate == gps[i].overTimeEndDate) == null)
{
rest.Add(gps[i]);
}
}
Dictionary的用法:
Dictionary<string, List<HRWorkOverTimeDto>> stuGroup = new Dictionary<string, List<HRWorkOverTimeDto>>();
List<HRWorkOverTimeDto> gps = DataFormat(gpst);
//将时间相同的加班人员分组
foreach (HRWorkOverTimeDto item in gps)
{
if (!stuGroup.Keys.Contains(item.overTimeStartDate.ToString() + item.overTimeEndDate.ToString()))
{
stuGroup.Add(item.overTimeStartDate.ToString() + item.overTimeEndDate.ToString(), new List<HRWorkOverTimeDto>());
}
stuGroup[item.overTimeStartDate.ToString() + item.overTimeEndDate.ToString()].Add(item);//注意键能够经过多个字段的拼接去实现分组
}
//将人员相同时间不一样的加班人员分组
Dictionary<string, List<HRWorkOverTimeDto>> stuGroup2 = new Dictionary<string, List<HRWorkOverTimeDto>>();
foreach (HRWorkOverTimeDto item in rest)
{
if (!stuGroup2.Keys.Contains(item.overTimePeople_hrBaseName))
{
stuGroup2.Add(item.overTimePeople_hrBaseName, new List<HRWorkOverTimeDto>());
}
stuGroup2[item.overTimePeople_hrBaseName].Add(item);
}
Dictionary(using System.Collections.Generic)
Dictionary与HashTable的区别:
实例应用:
1.Dictionary<String,String> dy = new Dictionary<String,String>();
dy.Add("key1","value1");//添加 dy["key2"] = "value2";//若是键不存在也能够经过此方法来添加键/值对 dy.ContainsKey("key1"); //判断该键是否存在 dy.Clear();//清除全部 2. //Dictionary System.Collections.DictionaryEntry dic=new System.Collections.DictionaryEntry("key1","value1"); Dictionary fruit = new Dictionary(); //加入重复键会引起异常 fruit.Add(1, "苹果"); fruit.Add(2, "桔子"); fruit.Add(3, "香蕉"); fruit.Add(4, "菠萝"); //由于引入了泛型,因此键取出后不须要进行Object到int的转换,值的集合也同样 foreach (int i in fruit.Keys) { Console.WriteLine("键是:{0} 值是:{1}",i,fruit); } //删除指定键,值 fruit.Remove(1); //判断是否包含指定键 if (fruit.ContainsKey(1)) { Console.WriteLine("包含此键"); } //清除集合中全部对象 fruit.Clear(); } Dictionary<K,V> 和哈希表的对比 3. //HashTable System.Collections.Hashtable table=new System.Collections.Hashtable(); table.Add("table1",1); table.Add("table2",2); System.Collections.IDictionaryEnumerator d=table.GetEnumerator(); while(d.MoveNext()) { System.Console.WriteLine(d.Entry.Key); } Dictionary与HashTable的异同点: 异同点 Dictionary<K,V> HashTable 不一样点 对所保存元素作类型约束 能够增长任何类型 添加/读取无须拆箱、装箱 添加/读取须要拆箱、装箱 相同点 经过key获取value ,添加元素方法相同 ,删除元素方法相同 ,遍历方法相同。 对于值类型,特定类型(不包括Object)的Dictionary的性能优于Hashtable。