C#中两个List<TModel>中根据指定条件--判断并获取不同数据的数据集合2
方式一:Linq
List<Test> list = new List<Test>();
list.Add(new Test { score = 10, name = "张君宝" });
list.Add(new Test { score = 20, name = "刘惜君" });
list.Add(new Test { score = 20, name = "刘惜君" });
list.Add(new Test { score = 30, name = "八戒" }); int total = (from temp in list
where temp.name == "刘惜君"
select temp.age).Count(); int score = (from temp in list
where temp.name == "刘惜君"
select temp.score).Sum();
this.textBox1.Text = "刘惜君:" + total + "人" + score + "score";
C#中两个List<TModel>中根据指定条件获取不同对象 续
原始需求:已经插入的数据不再重复插入(所有数据中排除已有数据,不存在数据以新对象形式存储在对象三中)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms; namespace StartExe
{
public partial class Form1 : Form
{
private List<Test> AllData = new List<Test>();
private List<Test> PartData = new List<Test>();
public Form1()
{
InitializeComponent(); AllData.Add(new Test { score = 10, name = "001" });
AllData.Add(new Test { score = 20, name = "002" });
AllData.Add(new Test { score = 30, name = "003" });
AllData.Add(new Test { score = 40, name = "004" }); PartData.Add(new Test { score = 30, name = "003" });
PartData.Add(new Test { score = 40, name = "004" });
} //方法:Linq
private void btnLinq_Click(object sender, EventArgs e)
{
LinqMethod(AllData, PartData);
} private void LinqMethod(List<Test> allData, List<Test> partData)
{
var query = from a in allData.AsEnumerable()
join p in partData.AsEnumerable()
on a.score equals p.score into tmp
from current in tmp.DefaultIfEmpty()
where current == null
select new
{
Name = a.name,
SCORE = a.score,
}; string str = string.Empty;
foreach (var item in query)
{
str += item.SCORE + "==" + item.Name + "\r\n";
} this.txtLinq.Text = "linq方式:\r\n" + str + query.Count().ToString();
//MessageBox.Show(str);
} //方法:Lambda
private void btnLambda_Click(object sender, EventArgs e)
{
LambdaMethod(AllData, PartData);
} private void LambdaMethod(List<Test> AllData, List<Test> PartData)
{
var query = AllData.AsEnumerable()
.GroupJoin
(
PartData.AsEnumerable(),
x => x.score, y => y.score, (x, y) => y.DefaultIfEmpty()
.Where(w => w == null).
Select(z => new { SCORE = x.score, Name = x.name })
).SelectMany(x => x); string str = string.Empty;
foreach (var item in query)
{
str += item.SCORE + "==" + item.Name + "\r\n";
} this.txtLambda.Text = "lambda方式:\r\n" + str + query.Count().ToString();
//MessageBox.Show(str);
}
} public class Test
{
public int age { get; set; }
public string name { get; set; }
public int score { get; set; }
}
}
运行效果如下所示:

C#中两个List<TModel>中根据指定条件--判断并获取不同数据的数据集合2的更多相关文章
- php 中两种获得数据库中 数据条数的方法
一种是传统的利用mysql_num_rows()来计算 $sql="select * from news"; $res=mysql_query($sql); $number=mys ...
- oracle中两个服务器连接中sys密码修改问题
问题描述:orcl服务器要连接orclstd 想要sqlplus sys/410526@orclstd as sysdba 连接orclstd数据库,但是发现啥意思密码不对,就对sys密码进行重新设置 ...
- java中activiti框架中的排他网关使用方法,多条件判断
当排他网关的判断条件中出现多个条件时,需要注意,设置判断条件时,可能遇到,流向相同的任务,而判断条件的变量个数不同 那么,必须在后面的运行任务时,将所有的涉及到的变量都设置进任务中,只不过,如果这个任 ...
- zeromq中两个dealer 通过一个router进行通信
发现有童鞋不是很清楚ZMQ中的“请求-回复”模式中的ROUTER怎么用,所以简单介绍一下“请求-回复”模式的使用(最后付代码). 一.讲一讲 1.要使用zmq 通过一个router进行通信,你首先需要 ...
- jsp中两种include的区别【转】
引用文章:http://www.ibm.com/developerworks/cn/java/j-jsp04293/ http://www.cnblogs.com/lazycoding/archive ...
- sql一个表中两个字段合并求和
sql一个表中两个字段,合并求和 SELECT SUM(字段a+'.'+字段b) as total from TABLE
- C语言中两位ASCII码可以表示汉字
最近偶然有人问到这个相关字符编码的问题,所以百度了下参考了这两个资料,进行了简单分析. ******************************************************** ...
- (转)C#中两个问号和一个问号 ??
小问题难倒很多人.今天发现了这个问题,搜了很长时间才看到记录下. 实例:dt.Columns.Add(firstRow.GetCell(i).StringCellValue ?? string.For ...
- mysql源码:关于innodb中两次写的探索
两次写可以说是在Innodb中很独特的一个功能点,而关于它的说明或者解释非常少,至于它存在的原因更没有多少文章来说,所以我打算专门对它做一次说明. 首先说明一下为什么会有两次写这个东西:因为innod ...
随机推荐
- 最新 中钢网java校招面经 (含整理过的面试题大全)
从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿.中钢网等10家互联网公司的校招Offer,因为某些自身原因最终选择了中钢网.6.7月主要是做系统复习.项目复盘.LeetCo ...
- CentOS系统下Tomcat的优化
一.JVM内存优化(线程优化) vim ./bin/catalina.sh 在catalina.sh文件中添加以下配置: JAVA_OPTS="-server -Xms128m -Xmx12 ...
- Shuffle 机制
1. 概述 Map 方法之后,Reduce 方法之前的数据处理过程称之为 Shuffle. 2. Partition 分区 需求:要求将统计结果按照条件输出到不同文件中(分区).比如:将统计结果按照手 ...
- linux系统下,在用户空间应用程序中模拟发送系统键盘事件
Linux 有自己的 input 子系统,可以统一管理鼠标和键盘事件. 如果想模拟键盘事件,但是系统没有键盘设备该如何是好? 基于输入子系统实现的 input 可以方便的在用户空间模拟鼠标和键盘事件. ...
- 【转】sql server迁移到mysql
SQL Server转换为MySQL工具,用了一下 感觉蛮不错的. 下载地址:https://www.jb51.net/softs/209207.html#downintro2 分享上来,同时也以便记 ...
- Spring Boot系列教程十一: Mybatis使用分页插件PageHelper
一.前言 上篇博客中介绍了spring boot集成mybatis的方法,基于上篇文章这里主要介绍如何使用分页插件PageHelper.在MyBatis中提供了拦截器接口,我们可以使用PageHelp ...
- 解读PHP面试-高并发解决方案类考察点
整理自慕课网360大牛全面解读PHP面试 ,购买链接:https://coding.imooc.com/class/133.html 1.高并发和大流量解决方法 真题回顾 PHP如何解决高并发大流量问 ...
- S03_CH06_AXI_VDMA_OV7725摄像头采集系统
S03_CH06_AXI_VDMA_OV7725摄像头采集系统 本课程将对Xilinx提供的一款IP核--AXI VDMA(Video Direct Memory Access) 进行详细讲解,为后续 ...
- javascript移动端 电子书 翻页效果
1.后端给一长串的纯文本 2.前端根据屏幕的高度,将文本切割为 n 页 3.使用插件 turn.js 将切割好的每页,加上翻书效果 <!DOCTYPE html> <html lan ...
- php底层变量存储
变量存储 php的变量使用一个结构体 zval来保存的,在Zend/zend.h中我们可以看到zval的定义 struct _zval_struct { /* Variable information ...