柚子快报激活码778899分享:c#高级写法

http://yzkb.51969.com/

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>里面的一个值v是否在其里面的字典的值中

static void Main(string[] args)

{

List> listDic = new List>();

Dictionary dic = new Dictionary();

dic.Add("88", "a");

dic.Add("123", "b");

Dictionary dic2 = new Dictionary();

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> listDic = new List>();

Dictionary dic = new Dictionary();

dic.Add("88", "a");

dic.Add("123", "b");

Dictionary dic2 = new Dictionary();

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> ListDicSort(List listDic, string rank)

{

List> myListDic = new List>();

//普通写法

//for (int i = 0; i < listDic.Count; i++)

//{

// Dictionary o = (Dictionary)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> dic_dic = new Dictionary>();

Dictionary dic = new Dictionary();

dic.Add("name", "回复剂");

dic.Add("type", "药");

dic_dic.Add("0", dic);

Dictionary dic2 = new Dictionary();

dic2.Add("name", "以太");

dic2.Add("type", "药");

dic_dic.Add("1", dic2);

Dictionary dic3 = new Dictionary();

dic3.Add("name", "斧子");

dic3.Add("type", "武器");

dic_dic.Add("2", dic3);

Dictionary dic4 = new Dictionary();

dic4.Add("name", "斧子");

dic4.Add("type", "药");

dic_dic.Add("3", dic4);

//===================================================================//

//按照type类型进行分类并且生成出字典嵌套字典

Dictionary>> 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());

 

柚子快报激活码778899分享:c#高级写法

http://yzkb.51969.com/

好文推荐

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。

发表评论

返回顶部暗黑模式