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 ...
随机推荐
- [设计]GUI设计规范的规范
1. 前言 很多著名的软件企业都有GUI设计规范,如 Apple 和 Google,得益于这些设计规范(或者叫指南),设计者才可以开发出符合平台视觉及交互要求的UI.其实除了这些大公司,软件公司都最好 ...
- php 后端跨域请求
header("Access-Control-Allow-Origin: http://a.com"); // 允许a.com发起的跨域请求 //如果需要设置允许所有域名发起的跨域 ...
- GeoJSON JS判断某一点是否在某一区域范围之内
GeoJSON JS判断某一点是否在某一区域范围之内 算法: function isInPolygon(checkPoint, polygonPoints) { var counter = 0; va ...
- 【django之博客系统开发】
一.项目简介 使用django开发一套博客系统,参考博客园. 需求如下: 项目结构: 二.全部代码 from django.db import models # Create your models ...
- http状态码是什么,有什么用,在哪里查看,分别代表什么意思?
写在前面: 当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求.当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含HTTP状态码的信息头用以响应浏览器的请求.本文主要是:关于ht ...
- 戴尔R720xd服务器系统安装
型号:R720xd 开启服务器,Ctrl+R进入raid配置 配置完raid后F2对硬盘进行格式化 保存并重启 F11进入BIOS选项设置U盘启动 选择U盘启动 开始进行系统安装!
- 【原创】开启PowerShell远程管理
非域网络,开启PowerShell远程管理,命令如下: 以下操作,PS命令窗口,必须都以管理员省份执行. Step 1: 机器A和B,分别开启PowerShell远程管理服务A = 192.168.3 ...
- Object的方法
1.Object.assign() 方法用于将所有可枚举属性的值从一个或多个源对象复制到目标对象.它将返回目标对象. ES2015引入的 ,且可用polyfilled.要支持旧浏览器的话,可用使用jQ ...
- eclipse打包
- Redis Setex命令
Redis SETEX命令用于在Redis键中的指定超时,设置键的字符串值. 返回值 字符串,如果在键中设置了值则返回OK.如果值未设置则返回 Null. 语法 下面是Redis SETEX命令的基本 ...