方式一: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的更多相关文章

  1. php 中两种获得数据库中 数据条数的方法

    一种是传统的利用mysql_num_rows()来计算 $sql="select * from news"; $res=mysql_query($sql); $number=mys ...

  2. oracle中两个服务器连接中sys密码修改问题

    问题描述:orcl服务器要连接orclstd 想要sqlplus sys/410526@orclstd as sysdba 连接orclstd数据库,但是发现啥意思密码不对,就对sys密码进行重新设置 ...

  3. java中activiti框架中的排他网关使用方法,多条件判断

    当排他网关的判断条件中出现多个条件时,需要注意,设置判断条件时,可能遇到,流向相同的任务,而判断条件的变量个数不同 那么,必须在后面的运行任务时,将所有的涉及到的变量都设置进任务中,只不过,如果这个任 ...

  4. zeromq中两个dealer 通过一个router进行通信

    发现有童鞋不是很清楚ZMQ中的“请求-回复”模式中的ROUTER怎么用,所以简单介绍一下“请求-回复”模式的使用(最后付代码). 一.讲一讲 1.要使用zmq 通过一个router进行通信,你首先需要 ...

  5. jsp中两种include的区别【转】

    引用文章:http://www.ibm.com/developerworks/cn/java/j-jsp04293/ http://www.cnblogs.com/lazycoding/archive ...

  6. sql一个表中两个字段合并求和

    sql一个表中两个字段,合并求和 SELECT SUM(字段a+'.'+字段b) as total  from TABLE

  7. C语言中两位ASCII码可以表示汉字

    最近偶然有人问到这个相关字符编码的问题,所以百度了下参考了这两个资料,进行了简单分析. ******************************************************** ...

  8. (转)C#中两个问号和一个问号 ??

    小问题难倒很多人.今天发现了这个问题,搜了很长时间才看到记录下. 实例:dt.Columns.Add(firstRow.GetCell(i).StringCellValue ?? string.For ...

  9. mysql源码:关于innodb中两次写的探索

    两次写可以说是在Innodb中很独特的一个功能点,而关于它的说明或者解释非常少,至于它存在的原因更没有多少文章来说,所以我打算专门对它做一次说明. 首先说明一下为什么会有两次写这个东西:因为innod ...

随机推荐

  1. laravel构建联合查询

    参考:http://laravelacademy.org/post/126.html DB门面可以指定不同的数据库连接(通过connection方法) /** * @param $login_uid ...

  2. 【Leetcode_easy】704. Binary Search

    problem 704. Binary Search solution: class Solution { public: int search(vector<int>& nums ...

  3. 第八章 拦截器机制——《跟我学Shiro》

    转发地址:https://www.iteye.com/blog/jinnianshilongnian-2025656 博客分类: 跟我学Shiro 跟我学Shiro  目录贴:跟我学Shiro目录贴 ...

  4. iOS-代理设计模式delegate和protocol

    充当代理的步骤: 首先要明确谁请别人代理,谁当别人的代理 1> 请代理三部曲: 1 写一个协议protoc,把自己不方便做的事列出来(@protocol  studentDelegate < ...

  5. 【c# 学习笔记】显示接口实现方法

    在接口  一张中,使用了隐式的接口实现方式,即在实现代码中没有制定实现那个接口中的CompareTo方法.相应地,自然就有显式的 接口实现方式,它指的是在实现过程中,明确指出实现哪一个接口中的哪一个方 ...

  6. 火狐低版本中显示时间格式为:yyyy-MM-dd hh:mm:ss,出现NaN

    在低版本的火狐(43以下)和IE8中,显示时间格式为:yyyy-MM-dd hh:mm:ss,会出现NaN:原因是只支持yyyy/MM/dd hh:mm:ss; 所以在new Date('2018-0 ...

  7. 守卫者的挑战(据说在bzoj有但我没找到)

    芒果君:一看就是概率dp(可是我不会啊,就算再裸也不会啊).然后先从最后想,能够满足题意的状态是 挑战次数>=L,获得价值>=0,那一定有f[总挑战数i][挑战成功数j][价值k].转移很 ...

  8. springboot与redis

    该项目已上传至GitHub:https://github.com/xiaostudy/springboot_redis 用到的框架技术有:springboot.mybatis.shiro.redis ...

  9. VC++类型转换

    一.其他数据类型转换为字符串 短整型(int) itoa(i,temp,10):///将i转换为字符串放入temp中,最后一个数便是十进制 itoa(i,temp,2):///按二进制方式转换 长整型 ...

  10. PAT(B) 1017 A除以B(Java)

    题目链接:1017 A除以B 分析 读取输入的A和B后,保存为字符串.模拟除法运算过程. 不要用BigInteger,因为会超时. 另外字符串经常要扩展(例如:append())的话,不要用Strin ...