c#高级写法
Linq的参考资料:https://www.cnblogs.com/liqingwen/p/5801249.html
1.判断str字符串中的逗号个数
string str = "1,2,3,4,5,6";
int count = str.Count(ch => ch == ',');
2.使用 where 子句来从数组中筛选那些具有特定长度的字符串
string[] words = { "the", "quick", "brown", "fox", "jumps" }; var query = from word in words where word.Length == 3 select word; foreach (var word in query) { Console.WriteLine(word); }
3.查询一个List<Dictionary<string, object>>里面的一个值v是否在其里面的字典的值中
static void Main(string[] args) { List<Dictionary<string, object>> listDic = new List<Dictionary<string, object>>(); Dictionary<string, object> dic = new Dictionary<string, object>(); dic.Add("88", "a"); dic.Add("123", "b"); Dictionary<string, object> dic2 = new Dictionary<string, object>(); dic2.Add("99", "b"); listDic.Add(dic); listDic.Add(dic2); //ToDictionary 转换成字典 var q = listDic.SelectMany(x => x.Where(y => y.Value == "b")).ToDictionary(a =>a.Key, a =>a.Value); foreach (var item in q) { Console.WriteLine(item.Key + "---" + item.Value); } Console.ReadLine(); }
如果需要输出其对应字典在List中的下标,则
static void Main(string[] args) { List<Dictionary<string, object>> listDic = new List<Dictionary<string, object>>(); Dictionary<string, object> dic = new Dictionary<string, object>(); dic.Add("88", "a"); dic.Add("123", "b"); Dictionary<string, object> dic2 = new Dictionary<string, object>(); dic2.Add("99", "b"); listDic.Add(dic); listDic.Add(dic2); //要下标的话,那就只能产生新对象了。 var q = listDic.Select((x, i) => new { index = i, dict = x.Where(y => y.Value.ToString() == "b") }); foreach (var item in q) { Console.WriteLine("index:{0}", item.index); foreach (var v in item.dict) { Console.WriteLine(v.Key + "---" + v.Value); } } Console.ReadLine(); }
4.字典合并
第一种
chanceAllDic = chanceSmallDic.Concat(chanceBigDic).ToDictionary(k => k.Key, v => v.Value);
第二种
buildDic = BattleJsonData.chanceSmallDic.Union(BattleJsonData.chanceBigDic).ToDictionary(k => k.Key, v => v.Value);
5.类型转换
public static List<Dictionary<string, object>> ListDicSort(List<object> listDic, string rank) { List<Dictionary<string, object>> myListDic = new List<Dictionary<string, object>>(); //普通写法 //for (int i = 0; i < listDic.Count; i++) //{ // Dictionary<string, object> o = (Dictionary<string, object>)listDic[i]; // myListDic.Add(o); //} //Linq写法 myListDic = (from x in listDic select (Dictionary < string,object>)x).ToList(); return myListDic.OrderByDescending(a => a[rank]).ToList(); }
6.自动分组
//============================模拟数据==================================// Dictionary<string, Dictionary<string, object>> dic_dic = new Dictionary<string, Dictionary<string, object>>(); Dictionary<string, object> dic = new Dictionary<string, object>(); dic.Add("name", "回复剂"); dic.Add("type", "药"); dic_dic.Add("0", dic); Dictionary<string, object> dic2 = new Dictionary<string, object>(); dic2.Add("name", "以太"); dic2.Add("type", "药"); dic_dic.Add("1", dic2); Dictionary<string, object> dic3 = new Dictionary<string, object>(); dic3.Add("name", "斧子"); dic3.Add("type", "武器"); dic_dic.Add("2", dic3); Dictionary<string, object> dic4 = new Dictionary<string, object>(); dic4.Add("name", "斧子"); dic4.Add("type", "药"); dic_dic.Add("3", dic4); //===================================================================// //按照type类型进行分类并且生成出字典嵌套字典 Dictionary<string, List<Dictionary<string, object>>> allDic = dic_dic.GroupBy(x => x.Value["type"]) .Select(g => new { key = g.Key, value = g.Select(s => s.Value) }) .ToDictionary(v => v.key.ToString(), v => v.value.ToList());