标签:style blog color io ar for 数据 div sp
当Dictionary中Key为实体时,进行用ContainsKey比较会发现,就算Model为一样但是结果比较为不存在;
故用以下代码即可,现将Keys转换ToArray(),再用数组的Contains进行比较;
但是一定要注意:要进行对实体类重写Equals,比较实体内某个字段相等,则存在;
var MenuModel = new Dictionary<MenuGroupModel, List<MenuButtonModel>>(); //忽略MenuModel增加数据 var navBarData = new Dictionary<MenuGroupModel, List<MenuButtonModel>>(); //忽略navBarData增加数据 foreach (var menuInfo in MenuModel) { //调用MenuGroupModel重写的Equals,MenuGroupSort相同即Model相同 if (navBarData.Keys.ToArray().Contains(menuInfo.Key)) { //是否存在 } }
namespace Model { public class MenuGroupModel { public string MenuGroupSort { get; set; } public string MenuGroupName { get; set; } /// <summary> /// 重写两个Model进行比较时:MenuGroupSort相等即Model相同 /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Equals(object obj) { return MenuGroupSort == (((MenuGroupModel)obj).MenuGroupSort); } } public class MenuButtonModel { public string Sort { get; set; } public string Name { get; set; } public string TargetModule { get; set; } public string Image { get; set; } /// <summary> /// 重写两个Model进行比较时:MenuGroupSort相等即Model相同 /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Equals(object obj) { return Sort == (((MenuButtonModel)obj).Sort); } } }
标签:style blog color io ar for 数据 div sp
原文地址:http://www.cnblogs.com/kingsly/p/3968259.html