C#中的Linq Intersect与Except方法使用实例

实例描述

现有某班学生的两份成绩,两份成绩中存在一些不一致的记录。需借助于编程方法找出这些不一致的记录。

实例代码

using System;
using System.Collections.Generic;
using System.Linq;
namespace IntersectAndExceptExp
{
  class Program
  {
    static void Main(string[] args)
    {
      List<Student> studentList1 = newList<Student>() {
        new Student(){StudentId=1,Score=64},
        new Student(){StudentId=2,Score=85},
        new Student(){StudentId=3,Score=78},
        new Student(){StudentId=4,Score=94},
        new Student(){StudentId=5,Score=90}
      };
      List<Student> studentList2 = newList<Student>() {
        new Student(){StudentId=1,Score=64},
        new Student(){StudentId=2,Score=80},
        new Student(){StudentId=3,Score=78},
        new Student(){StudentId=4,Score=94},
        new Student(){StudentId=5,Score=95}
      };
      var both = studentList1.Intersect(studentList2,new StudentComparer());
      var diff1 =studentList1.Except(both, new StudentComparer());
      var diff2 =studentList2.Except(both, new StudentComparer());
      Console.WriteLine("-------------下面是两份成绩中不同的记录--------------");
      Console.WriteLine("-------------第一份学生成绩--------------");
      foreach (var s in diff1)
      {
        Console.WriteLine("StudentId:"+s.StudentId+";Score:"+s.Score);
      }
      Console.WriteLine("-------------第一份学生成绩--------------");
      foreach (var s in diff2)
      {
        Console.WriteLine("StudentId:"+ s.StudentId + ";Score:" + s.Score);
      }
    }
  }
  public class Student
  {
    public int StudentId { get; set; }
    public int Score { get; set; }
  }
  public class StudentComparer : IEqualityComparer<Student>
  {
    public bool Equals(Student x, Studenty)
    {
      if (Object.ReferenceEquals(x, y)) returntrue;
      return x != null && y != null&& x.StudentId == y.StudentId && x.Score == y.Score;
    }
    public int GetHashCode(Student obj)
    {
      int hashStudentId =obj.StudentId.GetHashCode();
      int hashScore =obj.Score.GetHashCode();
      return hashStudentId ^ hashScore;
    }
  }
}

代码说明

先使用Intersect方法生成两份记录的交集,该方法会使用传入的比较器对值进行比较决定记录是否相同。基于前步生成的交集,再使用Except方法找出两份记录中不一致的记录,该方法同样使用传入的比较器对值进行比较决定记录是否相同。

执行结果

(0)

相关推荐

  • C#中Linq延迟查询的例子

    提出问题 下面所给代码编译时正常,但是执行时会出错,请指出程序在执行时能够执行到编号为(1)(2)(3)的代码行中的哪一行. using System; using System.Collections.Generic; using System.Linq; namespace DeferredExecutionExp { class Program { static void Main(string[] args) { List<Student> studentList = new List

  • C#中Linq查询基本操作使用实例

    摘要:本文介绍Linq查询基本操作(查询关键字) - from 子句 - where 子句 - select子句 - group 子句 - into 子句 - orderby 子句 - join 子句 - let 子句 - 复合from子句 - 在某些情况下,源序列中的每个元素本身可能是序列(集合),也可能包含序列 - 用语访问单个数据库中的内部集合 - 使用多个from字句执行连接 - 可以包含多个可从独立数据源生成补充查询的from字句 复合(顾名思义就是有多from的字句)实例: 复制代码

  • C#中的Linq to Xml详解

    前言 我相信很多从事.NET开发的,在.NET 3.5之前操作XML会比较麻烦,但是在此之后出现了Linq to Xml,而今天的主人公就是Linq to Xml,废话不多说,直接进入主题. 一.生成Xml 为了能够在结构有一定的组织,笔者建议大家新建一个控制台项目,并且新建一个CreateXml类(以下部分都属于该类中). 并在其中写入以下属性: 复制代码 代码如下: public static String Path         {             get            

  • c#数据绑定之linq使用示例

    XAML: 复制代码 代码如下: <Grid>            <Grid.ColumnDefinitions>                <ColumnDefinition Width="2*"/>                <ColumnDefinition Width="8*"/>            </Grid.ColumnDefinitions>            <G

  • C# LINQ to XML应用介绍

    W3C制定了XML DOM标准,.Net为了支持W3C的标准,从1.1版本开始就引入了XmlDocument类.我在前一篇博客中,介绍了如何使用XmlDocument类来对XML文档进行操作.后来 .Net又引入了LINQ,于是LINQ to XML也就应运而生,所以在.Net中,不仅可以用W3C XML DOM标准,还可以使用LINQ to XML来操作XML文档.下面就来简单介绍一下如何使用LINQ to XML. (一) 加载 加载XML比较常用的有三种方法: 复制代码 代码如下: pub

  • c#使用linq技术创建xml文件的小例子

    复制代码 代码如下: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.IO;using System.Xml;using System.Xml.Linq; namespa

  • C#使用linq对数组进行筛选排序的方法

    本文实例讲述了C#使用linq对数组进行筛选排序的方法.分享给大家供大家参考.具体如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace OrderQueryResults { class Program { static void Main(string[] args) { string[] names = {"kaka","kun

  • C# Linq读取XML文件的实例

    1.示例XML文件:Demo.xml 复制代码 代码如下: <?xml version="1.0" encoding="utf-8" ?><note>  <conf>    <to>infozero</to>    <from>lerroy</from>    <heading>测试信息</heading>    <body>第一条测试信息<

  • C#操作LINQ to SQL组件进行数据库建模的基本教程

    建立实体类 使用LINQ to SQL时,需要首先建立用于映射数据库对象的模型,也就是实体类.在运行时,LINQ to SQL 根据LINQ表达式或查询运算符生成SQL语句,发送到数据库进行操作.数据库返回后,LINQ to SQL负责将结果转换成实体类对象. 建立实体类的方法有很多,例如LINQ to SQL设计器,手动编码建立,使用XML文件映射,使用命令行工具SqlMetal生成等.其中最方便的就是LINQ to SQL设计器. 1.使用LINQ to SQL设计器建立实体类       

  • C# linq查询之动态OrderBy用法实例

    本文实例讲述了C# linq查询之动态OrderBy用法.分享给大家供大家参考.具体分析如下: groupList是原始数据集合,List<T> sortOrder是排序类型,desc 或者asc sortName是排序属性名称 1.使用反射. private static object GetPropertyValue(object obj, string property) { System.Reflection.PropertyInfo propertyInfo = obj.GetTyp

  • C#使用linq语句查询数组中以特定字符开头元素的方法

    本文实例讲述了C#使用linq语句查询数组中以特定字符开头元素的方法.分享给大家供大家参考.具体如下: 下面的代码查询数组中以字母k开头的元素 using System; using System.Collections.Generic; using System.Linq; using System.Text; static void Main(string[] args) { string[] names = {"kaka","kunka","kuma

  • C#使用LINQ中Enumerable类方法的延迟与立即执行的控制

    延时执行的Enumerable类方法 LINQ标准查询运算法是依靠一组扩展方法来实现的.而这些扩展方法分别在System.Linq.Enumerable和System.Linq.Queryable这连个静态类中定义. Enumerable的扩展方法采用线性流程,每个运算法会被线性执行.这种执行方法如果操作类似关系型数据库数据源,效率会非常低下,所以Queryable重新定义这些扩展方法,把LINQ表达式拆解为表达式树,提供程序就可以根据表达式树生成关系型数据库的查询语句,即SQL命令,然后进行相

  • C#使用linq查询大数据集的方法

    本文实例讲述了C#使用linq查询大数据集的方法.分享给大家供大家参考.具体如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LargeNumberQuery { class Program { static void Main(string[] args) { int[] numbers = CreateNumbers(7384738); Con

  • C#使用linq计算执行元素在列表中出现次数的方法

    本文实例讲述了C#使用linq计算执行元素在列表中出现次数的方法.分享给大家供大家参考.具体如下: 这是使用linq计算元素在列表中出现的次数,调用方法非常简单,和sql语句很像 复制代码 代码如下: // Count the number of times an item appears in this list public static int CountTimes<T>(this List<T> inputList, T searchItem) { return ((fro

  • c#中Linq to Sql 增删除的实例

    抽像类:   复制代码 代码如下: public abstract class AbUserAll    {        public abstract IQueryable<User_ALL> FindUserAll();        public abstract User_ALL FindUserAllById(int userid);        public abstract void Add(User_ALL user);        public abstract voi

随机推荐