Unity(C#)Linq 操作

编程入门 行业动态 更新时间:2024-10-12 05:47:36

Unity(C#)Linq <a href=https://www.elefans.com/category/jswz/34/1770947.html style=操作"/>

Unity(C#)Linq 操作

  • 按照条件查询:from in where select 的用法
 // 例如:获取某班级所有男生成绩总和int scoreSum = (from student in classStudentswhere student.gender == "男"  // where:查询条件select student.score).Sum(); // select:选择对象
// 找出班级内所有男生boys=(from stud classStudents where stud.gender == "男" select stud).ToList();
  • 随机排序:
// 给集合、数组按照某种类型,随机排序
mlist = mlist.Where(it => it.type == 1).OrderBy(x => Guid.NewGuid()).ToList();
// 按照指定类型,随机取出集合中的某个元素
randomItem = mlist.Where(it => it.type == 1).OrderBy(x => Guid.NewGuid()).First();
  • 判断某集合中是否有满足条件的对象:
// 例如:三班学生中中有没有叫"小明"的同学
// 方式1:
bool result1 = classThreeStudent.Exists(student => student.name == "小明");
// 方式2:建议使用Exists判断,元素多了,Any判断效率不高。
bool result2 = classThreeStudent.Any(student => student.name == "小明");
  • 根据条件查找对象:
// 方式1:
Role xiaoMing = roleList.Find(r => r.name == "小明");// 方式2:
int index = roleList.FindIndex(r => r.name == "小明");
if (index == -1) {// 没有找到叫小明的角色
} else {Role xiaoMing = roleList[index];
}// 等等...

更多推荐

Unity(C#)Linq 操作

本文发布于:2024-03-08 11:02:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1720686.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:操作   Unity   Linq

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!