{
List<PModel> datas = new PModel().PModels(20);
// 筛选P1为空的数据
List<PModel> filter = datas.Where(x => string.IsNullOrEmpty(x.P1)).ToList();
filter = datas.FindAll(x => string.IsNullOrEmpty(x.P1));
filter = datas.Distinct().ToList();//按照所有属性进行去重
filter = datas.Distinct(new PModelEqualityComparer()).ToList();//按照指定属性进行去重
// 判断集合中是否存在P1为空的数据
bool exists = datas.Exists(x => string.IsNullOrEmpty(x.P1));
exists = datas.Any(x => string.IsNullOrEmpty(x.P1));
// 排序 按id升序
datas.Sort((x1, x2) => x1.Id.CompareTo(x2.Id));
// 排序 按id降序
datas.Sort((x1, x2) => { return x2.Id.CompareTo(x1.Id); });
// OrderBy排序需要重新赋值
datas = datas.OrderBy(x=> x.Id).ToList();
// 降序
datas = datas.OrderByDescending(x=> x.Id).ToList();
// 分页,Take函数如果数据行数不足limit返回所有数据
int page = 3, limit = 8;
filter = datas.Skip((page - 1) * limit).Take(limit).ToList();
// 筛选P1为空的数据行数
int count = datas.Where(x => string.IsNullOrEmpty(x.P1)).Count();
// 基于datas返回一个新的表单,返回datas的Id列
List<int> list1 = datas.Select(x => x.Id).ToList();
// 基于datas返回一个新的表单,返回datas的Id、P1列
var _datas = datas.Select(x => new { x.Id, x.P1 });
// 循环执行一个函数
datas.ForEach(x => { Console.WriteLine($"{x.Id},{x.P1}"); });
//或者这样:
Action<PModel> action = (x) => { Console.WriteLine($"{x.Id},{x.P1}"); };
datas.ForEach(action);
// 返回第一个符合条件的数据
Func<PModel, bool> func = x => string.Equals(x.P1, "P1");
PModel model = new PModel();
// 如果没有这样的数据,First会触发异常
model = datas.Where(func).First();
model = datas.First(func);
// 如果没有这样的数据,FirstOrDefault返回值是null
model = datas.Where(func).FirstOrDefault();
model = datas.FirstOrDefault(func);
// 如果没有这样的数据,Find返回值是null
model = datas.Find(x => func.Invoke(x));
// 符合条件的最后一个
model = datas.FindLast(x => func.Invoke(x));
List<FModel> fdatas = new FModel().FModels(10);
datas[0].Fid = 1;
datas[1].Fid = 1;
datas[2].Fid = 1;
datas[3].Fid = 2;
datas[4].Fid = 2;
// 连接查询
var union = datas.Join(fdatas, d => d.Fid, f => f.Id, (d, f) =>
new
{
d.Id,
d.P1,
d.P2,
d.Fid,
f.Name
});
// 循环执行操作
union.ToList().ForEach(x => Console.WriteLine(x));
var _datasx = datas.WhereX(x => x.Id == 0);
bool boolx = datas.First().CCC(x => x.Id == 0);
//var publisherRow = data.AsEnumerable().FirstOrDefault(x => x.Field<int>("PublishId") == 0);
//datas.FirstOrDefault(x=>x.e)
}
public class PModelEqualityComparer : IEqualityComparer<PModel>
{
public bool Equals(PModel x, PModel y)
{
return x.Id == y.Id;
}
public int GetHashCode(PModel x)
{
return x.Id.GetHashCode();
}
}