C#中Linq查詢List與DataTable和Dictionary
阿新 • • 發佈:2020-07-08
查詢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") == ""
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();