1.循环赋值ForEach
itemBillList.ToList().ForEach(a => { if (a.Sub_CName.IsNullOrEmpty()) a.Sub_CName = "未知"; });
2.分组查询group
quotationAllList:
U_Id QuoteNum
01 1
01 2
02 1
02 2
02 3
lastQuotationAllList = (
from quotationAll in quotationAllList
orderby quotationAll.QuoteNum descending
group quotationAll by quotationAll.U_Id
into userQuotationAllList
select userQuotationAllList.FirstOrDefault()
)
结果:lastQuotationAllList :
U_Id QuoteNum
01 2
02 3
//根据国家分组,查询顾客数大于5的国家名和顾客数
var obj=from c in Customers
group c by c.Country into g
where g.Count()>5
order by g.Count() descending
select new
{
国家=g.Key,
顾客数=g.Count()
};
//根据国家和城市分组,查询顾客覆盖的国家和城市
var obj= from c in Customers
group cby new {c.City,c.Country}into g
order by g.Country,g.City
select new
{
国家=g.Key.Country,
城市=g.Key.City
};
//按照是否超重条件分组,分别查询订单数量
var obj=from o in Orders
group o by new {条件=o.Freight>100} into g
select new
{
数量=g.Count(),
是否超重=g.Key.条件?“是”:“否”
}
3.查询 Where Select
List<ItemBill> lastBillQuotationList = billQuotationList .Where(o=> lastQuotationAllList.Count(X => X.Id == o.QA_Id) > 0).ToList();
4.读取DataTable里的数据
var lst = dt.Rows.Cast<DataRow>().GroupBy(dr => dr["Name"]).ToList();//根据列名Name分组
var maxGroup = 1000;
while (lst.Count > 0)
{
var stepGroup = lst.Take(Math.Min(maxGroup, lst.Count)).ToList();//去前1000条数据
stepGroup.ForEach(g => lst.Remove(g));//从lst里移除这1000条数据
var lstTmp = stepGroup.ToList();
while (lstTmp.Count > 0)
{
var stepRows = lstTmp.ToList();
stepRows.ForEach(dr => lstTmp.Remove(dr));
stepRows.ForEach(obj=>{ //数据处理})
}
}
5.连接串string.Join Select
List<string> cidList=new List<string>();
string str=string.Join(",", cidList.Select(o
=> string.Format("'{0}'", o)).ToArray()));
6.排序 OrderBy
IList<T_ProductType> model = new List<T_ProductType>();
//精准搜索从分词搜索中提前
IList<int> ptIds = models.Select(b => b.PT_ID).ToList();
model = model.OrderBy(b => ptIds.Contains(b.PT_ID) ? 0 : 1).ToList();
#region 完全匹配提到最前面
model = model.OrderBy(b => s == b.PT_Name ? 0 : 1).ToList();
#endregion
7.查询 构建匿名类
var obj=fromcincustomers wherec.Country=="France" && c.Count>5order by c.Countdescending,c.Countryascending
select new{
ID=c.CustomerID,
Contact=new
{
phone=c.phone,
link=c.link
}
IsActive=c.Count>100?"是":"否"
};
8.分页 按照每页10条记录,查询第二页的数据
var obj=(from c in Customers select c).Skip(10).Take(10);
9.合并union(相同的会过滤)、concat(相同的不会过滤)、Intersect(相交)、Except(排除相交项)
查询城市是A打头和城市包含A的顾客并按照顾客名字排序
var 连接并且过滤相同项=(from c in Customers where c.City.Contains("A") select c)
.Union
(from c in Customers where c.ContactName.StartsWith("A") select c).OrderBy(c=>c.ContactName)
10.子查询
//查询订单数超过5的顾客信息
var obj=from c in Customers
where
(from o in Orders group o by o.CustomerID into o where o.Count()>5 select o.Key).Contains(c.CustomerID)
select c;
11.in 操作
var obj =from c in Customers
where new string[]{"123","456"}.Contains(c.City)
select c;
12.join 内连接
//没有分类的产品查询不到
var obj = from p in Products
join c in Categories
on p.CategoryID equals c.CategoryID
select p.ProductName
13 外连接
//没有分类的产品也能查询到
var obj=from p in Products
join c in Categories
on p.CategoryID equals c.CategoryID
into pro
from x in pro.DefaultIfEmpty()
select p.ProductName