1.SQL中的in和not in语法对应的LINQ写法
List<testInfo> listAll = new List<testInfo>();
listAll.Add(new testInfo() { id = 1, name = "11", test = "111" });
listAll.Add(new testInfo() { id = 2, name = "22", test = "222" });
listAll.Add(new testInfo() { id = 3, name = "33", test = "333" });
listAll.Add(new testInfo() { id = 4, name = "33", test = "333" });
List<testInfo> listFind = new List<testInfo>();
listFind.Add(new testInfo() { id = 1, name = "44", test = "111" });
listFind.Add(new testInfo() { id = 2, name = "22", test = "222" });
listFind.Add(new testInfo() { id = 3, name = "33", test = "333" });
listFind.Add(new testInfo() { id = 4, name = "55", test = "333" });
//SQL中的in和not in语法对应的LINQ写法
//相当于查询语句:select * from listAll p where p.name not in(select f.name from listFind)
var a = (from p in listAll
where !(from f in listFind select f.name).Contains(p.name)
select p).ToList();
2.排序
list = (from p in list
orderby p.shipname ascending
select p).ToList();
3.分页查询
int pageSize = 20;//每页记录数
int pageIndex=1;//当前页
//分页
list = (from p in list
select p).Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
4.分页查询2
/// <summary>
/// 计算列表可以分多少页 (此方法可以自己改良,赶时间只能写这么烂的代码了)
/// </summary>
/// <param name="dataList">List</param>
/// <returns>总页数</returns>
private int GetListPageCount(List<string> dataList)
{
int pageCount = 1;
int pageSize = 1000;//每页一千条
int rowCount = dataList.Count;
decimal temp = (decimal)rowCount / (decimal)pageSize;
string[] arry1 = temp.ToString().Split('.');
int temp2 = 0, temp3 = 0;
Int32.TryParse(arry1[0], out temp2);
if (arry1.Length > 1)
{
Int32.TryParse(arry1[1], out temp3);
}
if (temp2 > 1)
{
pageCount = temp2;
}
if (temp3 > 0)
{
pageCount = temp2 + 1;
}
return pageCount;
}
/// <summary>
/// 返回List某页数据
/// </summary>
/// <param name="dataList">List</param>
/// <param name="pageIndex">某页</param>
/// <returns>返回某页数据</returns>
private List<string> GetListByPage(List<string> dataList, int pageIndex)
{
//每页多少记录
int pageSize = 1000;
//分页
var list = (from p in dataList
select p).Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList();
return list;
}
//------------------------使用-----------------------------------
{
int pageCount = 0;
pageCount = GetListPageCount(allDataList); //allDataList是需要分页的list
for (int i = 1; i <= pageCount; i++)
{
var pageDataList = GetListByPage(allDataList, i);//第i页数据
//处理方法()
}
}
//-------------------------------------------