Linq 对象的比较 Contains,Max
Contains
public class StudentEquality : IEqualityComparer<Student>
{ public static StudentEquality Instance { get; set; } private StudentEquality()
{ } static StudentEquality()
{
Instance = new StudentEquality();
} public bool Equals(Student x, Student y)
{
if (x == null || y == null)
{
return false;
} if (x.StudentID == y.StudentID)
{
return true;
} return false;
} public int GetHashCode(Student obj)
{
return obj.GetHashCode();
}
}
List<Student> studentList = new List<Student>() {
new Student() { StudentID = , StudentName = "Zohn", StandardID = },
new Student() { StudentID = , StudentName = "Moin", StandardID = },
new Student() { StudentID = , StudentName = "Bill", StandardID = },
new Student() { StudentID = , StudentName = "Ram1" , StandardID = },
new Student() { StudentID = , StudentName = "Ron1123", StandardID = }
}; Student s = new Student() { StudentID = , StudentName = "Ron1123", StandardID = };
bool res = studentList.Contains(s, StudentEquality.Instance);
Console.WriteLine("Contains = " + res);// True
Max
public class StudentComparable : IComparable<StudentComparable>
{
public Student Student { get; set; } public int CompareTo(StudentComparable other)
{
if (Student.StudentName.Length > other.Student.StudentName.Length)
{
return ;
} if (Student.StudentName.Length == other.Student.StudentName.Length)
{
return ;
}
return -;
}
}
IList<Student> studentList = new List<Student>() {
new Student() { StudentID = , StudentName = "Zohn", StandardID = },
new Student() { StudentID = , StudentName = "Moin", StandardID = },
new Student() { StudentID = , StudentName = "Bill", StandardID = },
new Student() { StudentID = , StudentName = "Ram1" , StandardID = },
new Student() { StudentID = , StudentName = "Ron1123", StandardID = }
};
//选出最长的名字的 Student
StudentComparable max = studentList.Max(m => new StudentComparable { Student = m });
Console.WriteLine(JsonConvert.SerializeObject(max.Student));
Console.Read();
Linq 对象的比较 Contains,Max的更多相关文章
- Newtonsoft.Json.Linq对象读取DataSet数据
Newtonsoft.Json.Linq对象读取DataSet数据: private void button4_Click(object sender, EventArgs e) { ...
- Linq中max min sum avarage count的使用
一.Max最大值 static void Main(string[] args) { //Max求最大值 ,,,,,,,,,}; //方法1 Linq语句+Linq方法 var result = (f ...
- 【转】Linq Group by
http://www.cnblogs.com/death029/archive/2011/07/23/2114877.html 1.简单形式: var q = from p in db.Product ...
- Linq使用Group By 1
Linq使用Group By 1 1.简单形式: var q = from p in db.Products group p by p.CategoryID into g select g; 语句描述 ...
- Linq操作
Linq使用Group By 1 1.简单形式: var q = from p in db.Products group p by p.CategoryID into g select g; 语句描述 ...
- linq 常用语句
自己练习的 switch (productDataAnalysisQuery.DataType) { : var data = (from hp in GPEcontext.hbl_product j ...
- LINQ(隐式表达式、lambda 表达式)
.NET 中一项突破性的创新是 LINQ(Language Integrated Query,语言集成查询),这组语言扩展让你能够不必离开舒适的 C# 语言执行查询. LINQ 定义了用于构建查询表达 ...
- LINQ to XML学习笔记
一.XML基础知识 1.XML:可扩展标记语言 Extensible Markup Language ,提供了一种保存数据的格式,数据可以通过这种格式很容易地在不同的应用程序之间实现共享. 2.使用X ...
- linq中的GroupBy总结
1.简单形式: var q = from p in db.Products group p by p.CategoryID into g select g; 语句描述:Linq使用Group By按C ...
随机推荐
- JAVA通过COM接口操作PPT
一. 背景说明 在Eclipse环境下,开发JAVA代码操作PPT,支持对PPT模板的修改.包括修改文本标签.图表.表格.满足大多数软件生成PPT报告的要求,即先收工创建好模板,在程序中修改模板数据. ...
- 读取MySQL存储二进制的语音、图片(Blob类型)
/** * 下载语音 * Remarks: * @throws Exception */ public void downloadYuyin() throws Exception { ...
- To Fill or Not to Fill (贪心)
转自:https://www.cnblogs.com/XBWer/p/3866486.html With highways available, driving a car from Hangzhou ...
- 28.Django cookie
概述 1.获取cookie request.COOKIES['key'] request.COOKIES.get('key') request.get_signed_cookie(key, defau ...
- Node与apidoc的邂逅——NodeJS Restful 的API文档生成
作为后台根据需求文档开发完成接口后,交付给前台(angular vue等)做开发,不可能让前台每个接口调用都去查看你的后台代码一点点查找.前台开发若不懂你的代码呢?让他一个接口一个接口去问你怎么调用, ...
- RESTful接口设计原则和优点
RESTful架构优点: 前后端分离,减少流量 安全问题集中在接口上,由于接受json格式,防止了注入型等安全问题 前端无关化,后端只负责数据处理,前端表现方式可以是任何前端语言(android,io ...
- .Net调用钉钉接口,实现发送企业消息功能
我在钉钉官网上看了下,关于调用钉钉接口,发送企业消息通知,他给的核心代码总共几行. 用到的一个接口引用 TopSDK.dll 下载网址https://open-doc.dingtalk.com/doc ...
- git将文件托管到github上遇到的问题
先来一问题描述: 执行:$ git push -u origin master 结果Warning: Permanently added the RSA host key for IP address ...
- VMware静态地址上网
虚拟机通过dhcp获取ip,当系统重启时可能导致ip变更,出现不必要的麻烦,以下是通过nat模式设置虚拟机静态ip同时能够上网的方式. 编辑VMware,依次点击“编辑”--“虚拟网络编辑器” 注:为 ...
- C语言--第0次作业
1.你认为大学的学习生活.同学关系.师生应该是怎样?请一个个展开描写. 学习生活 大学是一个过渡时间,它不同于高中与社会.我希望自己可以养成自主学习的习惯,也希望能在大学学习中找到属于自己的节奏,不被 ...