1.Find() & FindAll() 的使用
Find() 获取第一个符合条件的元素并返回该元素
FindAll() 获取所有符合条件的元素,并最终返回一个列表
namespace AlienDemo
{
class AlienDemoClass
{
static void Main()
{
// 获取字符串中尾部为“1”的字符串的列表
List<string> strList = new List<string>() { "Alien_1", "ware_1", "hello", "world" };
List<string> resultStrList = strList.FindAll(delegate (string str) {return str.EndsWith("1"); });
PrintListInfo(resultStrList);
// 获取列表中,元素大于33的所有元素列表
List<int> intList = new List<int>() { 12, 23, 34, 56 };
List<int> reIntList = intList.FindAll(delegate (int item) { return item > 33 ; });
PrintListInfo(reIntList);
// 获取列表中,第一个尾部为1的元素
string resultStr = strList.Find(delegate (string str) { return str.EndsWith("1"); });
Console.WriteLine("resultStr------>{0}", resultStr);
// 打印方法
static void PrintListInfo<T>(List<T> listName)
{
foreach (var item in listName)
{
Console.Write("{0} , ", item);
}
Console.WriteLine("\t");
}
}
}
}
Alien_1 , ware_1 ,
34 , 56 ,
resultStr------>Alien_1
2.Select() & Where()的使用
一般列表中的元素是对象的时候,可以通过Select & Where两个函数获取列表中对象的属性值列表
namespace AlienDemo
{
#region 测试Model
public class TestModel
{
public int Id { get; set; }
public String Name { get; set; }
public decimal Score { get; set; }
}
#endregion
class AlienDemoClass
{
static void Main()
{
#region list.Select
List<TestModel> selectList = new List<TestModel>();
selectList.Add(new TestModel { Id = 3, Name = "U3", Score = 30 });
selectList.Add(new TestModel { Id = 2, Name = "U2", Score = 20 });
selectList.Add(new TestModel { Id = 4, Name = "U4", Score = 40 });
selectList.Add(new TestModel { Id = 1, Name = "U1", Score = 10 });
#endregion
// 获取每个列表中元素的Id属性值,对应的列表
List<dynamic> selectResList = selectList.Select(
t => (dynamic)t.Id
).ToList();
PrintListInfo(selectResList);
// 获取每个元素的Id和Name属性值,对应的二级列表
selectResList = selectList.Select(t => (dynamic)new { t.Id, t.Name }).ToList();
PrintListInfo(selectResList);
static void PrintListInfo<T>(List<T> listName)
{
foreach (var item in listName)
{
Console.Write("{0} , ", item);
}
Console.WriteLine("\t");
}
}
}
}
//3 , 2 , 4 , 1
//{ Id = 3, Name = U3 } , { Id = 2, Name = U2 } , { Id = 4, Name = U4 } , { Id = 1, Name = U1 }
【特殊用法:】
如果Select()中有判断的部分,则最终返回的列表是布尔类型bool的列表
List<dynamic> selectResList = selectList.Select(
t => (dynamic)t.Id > 3
).ToList();
PrintListInfo(selectResList);
// False , False , True , False ,
3.Sort() 对类对象列表进行排序
namespace AlienDemo
{
public class TestModel
{
public int Id { get; set; }
public String Name { get; set; }
public decimal Score { get; set; }
}
class AlienDemoClass
{
static void Main()
{
#region list.Select
List<TestModel> selectList = new List<TestModel>();
selectList.Add(new TestModel { Id = 3, Name = "U3", Score = 30 });
selectList.Add(new TestModel { Id = 2, Name = "U2", Score = 20 });
selectList.Add(new TestModel { Id = 4, Name = "U4", Score = 40 });
selectList.Add(new TestModel { Id = 1, Name = "U1", Score = 10 });
#endregion
#region list.Select
// 根据列表中元素的Id属性,从小到大对类排序
// method1:
//selectList.Sort((x, y) => (x.Id - y.Id)) ;
// method2:
selectList.Sort((x, y) =>
{
return (x.Id - y.Id);
});
// 根据列表中元素的Id属性,从大到小对类排序
// method3:
//selectList.Sort((x, y) => x.Id > y.Id ? -1 : 0);
#endregion
foreach (TestModel item in selectList)
{
Console.Write("{0} , ",item.Id);
}
Console.WriteLine("\t");
}
}
}
4.Exists() 判断对象列表中是否有符合条件的对象
namespace AlienDemo
{
#region 测试Model
public class TestModel
{
public int Id { get; set; }
public String Name { get; set; }
public decimal Score { get; set; }
}
#endregion
class AlienDemoClass
{
static void Main()
{
#region list.Select
List<TestModel> selectList = new List<TestModel>();
selectList.Add(new TestModel { Id = 3, Name = "U3", Score = 30 });
selectList.Add(new TestModel { Id = 2, Name = "U2", Score = 20 });
selectList.Add(new TestModel { Id = 4, Name = "U4", Score = 40 });
selectList.Add(new TestModel { Id = 1, Name = "U1", Score = 10 });
#endregion
// 如下多种写法均可
// 最原始写法
//var exists = selectList.Exists((TestModel t) => t.Id == 2 ? true : false).ToString();
// 省略了三元表达式,最终效果一样
//var exists = selectList.Exists((TestModel t) => t.Id == 2).ToString();
// 省略了列表中元素t的声明
//var exists = selectList.Exists(t => t.Id == 2).ToString();
// 对布尔类型结果直接打印,省去了转string过程
var exists = selectList.Exists(t => t.Id == 2);
}
}
}
// True========>System.Boolean