C#中Linq查询List与DataTable和Dictionary

 

查询DataTable返回List

 List<string> listNation = dtNation.AsEnumerable().Select(d => d.Field<string>("MZMC").Trim()).Distinct().ToList();

查询DataTable返回string

 string strB = (from DataRow r in dtA.Rows
                              where r.Field<string>("A") == "1034"
                              select r.Field<string>("B")).FirstOrDefault();

查询DataTable返回DataTable

DataTable dtB= (from r in dtA.AsEnumerable()
                       where r.Field<string>("XY").Trim().Equals("A")
                       select r).CopyToDataTable<DataRow>();

查询List返回Dictionary

Dictionary<string, string> DictionaryB = ListA.FeeSubject.Where(x => !string.IsNullOrEmpty(x.Code)).Select(x => new { Key = x.FeeCode, Value = x.Code }).Distinct().ToDictionary(c => c.Key, c => c.Value);

List集合取差集

 List<string > C = A.Except(B).ToList();

 移除

ComboBoxPar.VmCommun.RemoveAll(x => x.CommunityName.Equals("全部")) ;

 

posted @ 2020-07-08 16:58  be--yourself  阅读(1265)  评论(0编辑  收藏  举报