数据元素合并
byte [] bSend = bytA.Concat(bytB).ToArray();
List集合中查找元素
//字符串查询
var BytList = new List<string>(){ "ad","bb" };
var byt = BytList.Where(x => x.IndexOf("a") >= 0).ToList();
var BytList = new List<object>(){...};
//单个条件
var BytA = BytList.Where(it => it.Key == 1).ToList();
//多个条件
var BytB = BytList.Where(it => it.Key == 1 && it.Name == "Q").ToList();
多个集合合并
var ListIDs = lstID2.Union(lstID3).Union(lstID4).ToList();
从DataTable取某列值转换为List
List<int> lstID = (from d in dt.AsEnumerable() select d.Field<int>("ID")).ToList();
根据逗号进行字符串分隔,并去除空格和NULL
var BytList = new List<string>(ary.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries));
对数组以逗号分隔
string.Join(",", 数组)
删除集合中所有Name等于'sa'的数据
List.RemoveAll((arg) => arg.Name == 'sa');
集合转数组
string[] str=list.ToArray();
数组转集合
List<string> list=new List<string>(数组);
对集合根据条件搜索某属性集合
List<Nma> list = new List<Nma>();
list.Add(new Nma { Name = "sa", Age = 1 });
list.Add(new Nma { Name = "sb", Age = 2 });
list.Add(new Nma { Name = "sc", Age = 3 });
List<string> strary = list.Where(people => people.Age > 1).Select(people => people.Name).ToList();
根据条件搜索集合
List<Nma> sb = list.Where(people => people.Age > 1).ToList();
List排序
List<string> tmpList = new List<string>();
tmpList.Add("a");
tmpList.Add("b");
tmpList.Add("c");
tmpList.Sort();//排序
tmpList.Reverse();//反转序列
tmpList.Sort((x, y) => x.CompareTo(y));//升序
tmpList.Sort((x, y) => -x.CompareTo(y));//降序
public class cls
{
public int Id { set; get; }
public string Name { set; get; }
}
List<cls> tmpList2 = new List<cls>() {
new cls(){ Id=0,Name="a"},
new cls(){ Id=0,Name="b"},
new cls(){ Id=0,Name="c"}
};
var list = tmpList2.OrderBy(o => o.Id).ToList();//升序
var list2 = tmpList2.OrderByDescending(o => o.Id).ToList();//降序
对Dictionary进行排序
dicparas.Add("a","dadfsfse");
dicparas.Add("b", "dadfsfse");
dicparas.Add("c", "dadfsfse");
//正序
Dictionary<string, string> dic1_SortedByKey = dicparas.OrderBy(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
//倒序
Dictionary<string, string> dic2_SortedByKey = dicparas.OrderByDescending(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
对Dictionary循环取值
foreach(KeyValuePair<int,string> item in myDictionary)
{/*item.Key, item.Value*/}
线程安全的集合
ConcurrentDictionary<string, Cls>
获取datatable记录集的平均值
var avg = datatable.AsEnumerable().Average<DataRow>(A => decimal.Parse(A["价格"].ToString()))
获取datatable记录集的最大值
decimal _max = datatable.AsEnumerable().Max<DataRow>(A => decimal.Parse(A["价格"].ToString()));
获取datatable记录集的最小值
decimal _min = datatable.AsEnumerable().Min<DataRow>(A => decimal.Parse(A["价格"].ToString()));
datatable某列转为集合或数字
List<string> lit = datatable.AsEnumerable().Select(d => d.Field<string>("arry")).ToList();
string[] ary = datatable.AsEnumerable().Select(d => d.Field<string>("arry")).ToArray();