C# Linq 常用 整理 Join

using System.Linq;

1.DataTable 获取列合计

fiel 为列。

1.1无查询条件

var sum = dt.AsEnumerable().Sum(s => s.Field<decimal>(fiel));

1.2有查询条件

var sum = dt.Select(fiel+" is not null").AsEnumerable().Sum(s => s.Field<decimal>(fiel));

 

 

2.List 获取对像

List<Player> listPlayers = new List<Player>();

if (listPlayers.Exists(c => c.gms_location == id)) 

Player p =listPlayers.First(c => c.gms_location == id);

 

3.List 合计列

3.1 数量

listPlayers.Where(a => a.gms_gign == "警察" &&a.isDie).Count();

3.2.合计列

List<wktype> rows;

var je = rows.Sum(x => x.je);

var cs = rows.Where().Sum(x => x.cs);

 

public class wktype         public string type { get; set; }         public Decimal cs { get; set; }

    }

 

4.List排序 检索 遍历

 4.1

List<Player> ps = listPlayers.Where(a => a.gms_gign == "警察").OrderBy(a=>a.gms_location).ToList<Player>();//OrderByDescending

foreach(Player p in ps)                     

                }

 

更新 : ps .ForEach(x => x.stock=-1)

带条件更新: info.ForEach(x => { if (x.stock == -1) { x.stock = 1; } });  

4.2                 where p.gms_gign == "警察"                 select p;

foreach(Player p in ps)                     

                }

 

List<int> arr = new List<int>() { 1, 2, 3, 4, 5, 6, 7 };             var s = (from v in arr where v > 3 select v).Sum();             var sss = arr.SelectMany<int, string>(a => { return new List<string>() { "a", a.ToString() }; });             MessageBox.Show(s.ToString());

 

选取一列,去重,转为字符串数组 string[]

var groups = map.listMap.Where(a => a.visible == "1").Select(o => o.mname).Distinct().ToArray();

或其他数组  .Distinct().ToArray();

var groups1 = map.listMap                 .Select(o => new { a = o.mname,b="aa" })                 .ToArray();

            ViewBag.groups1 = groups1;

C# Linq 常用 整理 Join_字符串数组

分组转List<string>

var groups1 = map.listMap                 .GroupBy(a => a.mname)

                .Select(o => o.Key);

            ViewBag.groups1 = groups1.ToList();

或其他类型

var groups1 = map.listMap                 .GroupBy(a => a.mname)

                .Select(o => new {key =o.Key});

            ViewBag.groups1 = groups1.ToList();

 

4.3 Join

string roles = string.Join(",", chkListRole.CheckedItems.Cast<string>().Select(a=>a.ToString()));

 


C# Linq 常用 整理 Join

using System.Linq;

1.DataTable 获取列合计

fiel 为列。

1.1无查询条件

var sum = dt.AsEnumerable().Sum(s => s.Field<decimal>(fiel));

1.2有查询条件

var sum = dt.Select(fiel+" is not null").AsEnumerable().Sum(s => s.Field<decimal>(fiel));

 

 

2.List 获取对像

List<Player> listPlayers = new List<Player>();

if (listPlayers.Exists(c => c.gms_location == id)) 

Player p =listPlayers.First(c => c.gms_location == id);

 

3.List 合计列

3.1 数量

listPlayers.Where(a => a.gms_gign == "警察" &&a.isDie).Count();

3.2.合计列

List<wktype> rows;

var je = rows.Sum(x => x.je);

var cs = rows.Where().Sum(x => x.cs);

 

public class wktype         public string type { get; set; }         public Decimal cs { get; set; }

    }

 

4.List排序 检索 遍历

 4.1

List<Player> ps = listPlayers.Where(a => a.gms_gign == "警察").OrderBy(a=>a.gms_location).ToList<Player>();//OrderByDescending

foreach(Player p in ps)                     

                }

 

更新 : ps .ForEach(x => x.stock=-1)

带条件更新: info.ForEach(x => { if (x.stock == -1) { x.stock = 1; } });  

4.2                 where p.gms_gign == "警察"                 select p;

foreach(Player p in ps)                     

                }

 

List<int> arr = new List<int>() { 1, 2, 3, 4, 5, 6, 7 };             var s = (from v in arr where v > 3 select v).Sum();             var sss = arr.SelectMany<int, string>(a => { return new List<string>() { "a", a.ToString() }; });             MessageBox.Show(s.ToString());

 

选取一列,去重,转为字符串数组 string[]

var groups = map.listMap.Where(a => a.visible == "1").Select(o => o.mname).Distinct().ToArray();

或其他数组  .Distinct().ToArray();

var groups1 = map.listMap                 .Select(o => new { a = o.mname,b="aa" })                 .ToArray();

            ViewBag.groups1 = groups1;

C# Linq 常用 整理 Join_字符串数组

分组转List<string>

var groups1 = map.listMap                 .GroupBy(a => a.mname)

                .Select(o => o.Key);

            ViewBag.groups1 = groups1.ToList();

或其他类型

var groups1 = map.listMap                 .GroupBy(a => a.mname)

                .Select(o => new {key =o.Key});

            ViewBag.groups1 = groups1.ToList();

 

4.3 Join

string roles = string.Join(",", chkListRole.CheckedItems.Cast<string>().Select(a=>a.ToString()));