Lambda中的一些方法的总结
public List<UserInfoBaseModel> GetNameByIDList(List<int> UserID)
{
var UserList = LoadRepository<User_Info>()
.GetModel()
.Where(x => UserID.Contains(x.UserInfoID))
.Select(i => new UserInfoBaseModel()
{
UserInfoId = i.UserInfoID,
UserName = i.UserName
})
.ToList();
return UserList;
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Lambda的各种语法
{
class Program
{
static void Main(string[] args)
{
List<Student> list = new List<Student>()
{
new Student(){ StudentID = , Name = "张飞", Age = , Gender = },
new Student(){ StudentID = , Name = "关羽", Age = , Gender = },
new Student(){ StudentID = , Name = "刘备", Age = , Gender = },
new Student(){ StudentID = , Name = "貂蝉", Age = , Gender = },
new Student(){ StudentID = , Name = "孙尚香", Age = , Gender = },
new Student(){ StudentID = , Name = "甄宓", Age = , Gender = },
new Student(){ StudentID = , Name = "大乔", Age = , Gender = },
new Student(){ StudentID = , Name = "小乔", Age = , Gender = },
new Student(){ StudentID = , Name = "马云禄", Age = , Gender = },
new Student(){ StudentID = , Name = "蔡琰", Age = , Gender = },
new Student(){ StudentID = , Name = "祝融", Age = , Gender = },
new Student(){ StudentID = , Name = "关凤", Age = , Gender = },
new Student(){ StudentID = , Name = "隐藏", Age = , Gender = },
new Student(){ StudentID = , Name = "英雄", Age = , Gender = },
};
List<StudentExtension> list1 = new List<StudentExtension>()
{
new StudentExtension(){ ID = , StudentID = , Range = "蜀国", TeacherName = "张三"},
new StudentExtension(){ ID = , StudentID = , Range = "蜀国", TeacherName = "李四"},
new StudentExtension(){ ID = , StudentID = , Range = "蜀国", TeacherName = "王二"},
new StudentExtension(){ ID = , StudentID = , Range = "汉", TeacherName = "麻子"},
new StudentExtension(){ ID = , StudentID = , Range = "吴国", TeacherName = "王五"},
new StudentExtension(){ ID = , StudentID = , Range = "魏国", TeacherName = "赵六"},
new StudentExtension(){ ID = , StudentID = , Range = "吴国", TeacherName = "张三"},
new StudentExtension(){ ID = , StudentID = , Range = "吴国", TeacherName = "李四"},
new StudentExtension(){ ID = , StudentID = , Range = "蜀国", TeacherName = "王二"},
new StudentExtension(){ ID = , StudentID = , Range = "汉", TeacherName = "麻子"},
new StudentExtension(){ ID = , StudentID = , Range = "蜀国", TeacherName = "王五"},
new StudentExtension(){ ID = , StudentID = , Range = "蜀国", TeacherName = "赵六"},
new StudentExtension(){ ID = , StudentID = , Range = "蜀国", TeacherName = "赵六11"},
new StudentExtension(){ ID = , StudentID = , Range = "蜀国", TeacherName = "赵六22"},
new StudentExtension(){ ID = , StudentID = , Range = "蜀国", TeacherName = "赵六33"},
}; ///select 返回一个匿名函数
var lambda1 = list.Select(i => new { i.Age, i.Name, i.StudentID, i.Gender });
foreach (var item in lambda1)
{
Console.WriteLine(item.StudentID + "\t" + item.Name + "\t" + item.Age + "\t" + item.Gender);
}
Console.WriteLine("select -----end");
///where
var lambda2 = list.Where(i => i.Age < ).Select(i => new { i.Age, i.Name, i.StudentID, i.Gender });
foreach (var item in lambda2)
{
Console.WriteLine(item.StudentID + "\t" + item.Name + "\t" + item.Age + "\t" + item.Gender);
}
Console.WriteLine("where -----end");
///OrderBy 升序
var lambda3 = list.OrderBy(i => i.Age);
foreach (var item in lambda3)
{
Console.WriteLine(item.StudentID + "\t" + item.Name + "\t" + item.Age + "\t" + item.Gender);
}
Console.WriteLine("OrderBy -----end");
///OrderByDescending 降序
var lambda4 = list.OrderByDescending(i => i.Age);
foreach (var item in lambda4)
{
Console.WriteLine(item.StudentID + "\t" + item.Name + "\t" + item.Age + "\t" + item.Gender);
}
Console.WriteLine("OrderByDescending -----end");
//group子句进行分组
var lambda5 = list.GroupBy(i => i.Age);
foreach (var item in lambda5)
{
Console.WriteLine(item.Key);
foreach (var item1 in item)
{
Console.WriteLine(item1.StudentID + "\t" + item1.Name + "\t" + item1.Age + "\t" + item1.Gender);
}
} ///多表链接
///lambda内连接
var lambda6 = list.Join(list1,
x => x.StudentID,//x对应的是list
c => c.StudentID,//c对应的是list1
(x, c) => new StudentView()//第三个参数对设置返回的数据类型
{
Age = x.Age,
Range = c.Range,
Name = x.Name,
Gender = x.Gender,
StudentID = x.StudentID,
TeacherName = c.TeacherName
}).ToList(); ///lambda分组链接
var lambda7 = list.GroupJoin(list1,
x => x.StudentID,//x对应的是list
c => c.StudentID,//c对应的是list1
(x, c) => new //第三个参数对设置返回的数据类型
{
Age = x.Age,
Name = x.Name,
Gender = x.Gender,
StudentID = x.StudentID,
V = c
}).ToList();
foreach (var item in lambda7)
{
Console.WriteLine(item.StudentID + "\t" + item.Name + "\t" + item.Age + "\t" + item.Gender + "我是Key");
foreach (var item1 in item.V)//item.v中是匹配到的数据集合
{
Console.WriteLine(item1.ID + "\t" + item1.Range + "\t" + item1.StudentID + "\t" + item1.TeacherName);
}
} Console.ReadLine();
}
} public class Student
{
[DisplayName("学生的ID")]
public int StudentID { get; set; }
[DisplayName("学生姓名")]
public string Name { get; set; }
[DisplayName("学生的年龄")]
public int Age { get; set; }
/// <summary>
/// 1男2女3未设置
/// </summary>
[DisplayName("学生的性别")]
public int Gender { get; set; } }
public class StudentExtension
{
public int ID { get; set; }
public int StudentID { get; set; }
public string Range { get; set; }
public string TeacherName { get; set; }
}
public class StudentView
{
public int StudentID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
/// <summary>
/// 1男2女3未设置
/// </summary>
public int Gender { get; set; }
public int ID { get; set; }
public string Range { get; set; }
public string TeacherName { get; set; }
}
}
Lambda中的一些方法的总结的更多相关文章
- Lambda 中如果构建一个查询条件,扔该Where返回我们需要的数据。
有一个需求,比如所 省市县 这三个查询条件 都可能有可能没有,但是我们的查询条件怎么构建呢 首先需要看一下 Lambda中Where这个方法需要什么参数 public static IEnumerab ...
- C#中的匿名方法
C#中的匿名方法是在C#2.0引入的,它终结了C#2.0之前版本声明委托的唯一方法是使用命名方法的时代.虽然在 C# 3.0 及更高版本中,Lambda 表达式取代了匿名方法,作为编写内联代码的首选方 ...
- 简谈 JavaScript、Java 中链式方法调用大致实现原理
相信,在 JavaScript .C# 中都见过不少链式方法调用,那么,其中实现该类链式调用原理,大家有没有仔细思考过?其中 JavaScript 类库:jQuery 中就存在大量例子,而在 C# 中 ...
- Lambda表达式与匿名方法
在C#2中,由于有了方法组,匿名方法,类型的协变和抗变,使得运用delegate变得很容易,在注册事件时代码变得简单易读,但是在C# 2中,代码仍然有点臃肿,大块的匿名方法会降低代码的可读性,一般我们 ...
- Java 8新特性探究(一) JEP126特性lambda表达式和默认方法
Lambda语法 函数式接口 函数式接口(functional interface 也叫功能性接口,其实是同一个东西).简单来说,函数式接口是只包含一个方法的接口.比如Java标准库中的java.la ...
- java代码之美(10)---Java8 Map中的computeIfAbsent方法
Map中的computeIfAbsent方法 Map接口的实现类如HashMap,ConcurrentHashMap,HashTable等继承了此方法,通过此方法可以在特定需求下,让你的代码更加简洁. ...
- [二] java8 函数式接口详解 函数接口详解 lambda表达式 匿名函数 方法引用使用含义 函数式接口实例 如何定义函数式接口
函数式接口详细定义 package java.lang; import java.lang.annotation.*; /** * An informative annotation type use ...
- 委托学习过程及委托、Lambda表达式和匿名方法的关系总结及事件总结
第一章,当开始学习委托的时候,我们会问什么是委托?为什么要学习委托? 一,什么是委托? 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法, ...
- Java 8 接口中的默认方法与静态方法
Java 8 接口中的默认方法与静态方法 1. 接口中的默认方法 允许接口中包含具有具体实现的方法,该方法称"默认方法",默认方法使用用 default 关键字修饰. public ...
随机推荐
- 【转】apache kafka技术分享系列(目录索引)
转自: http://blog.csdn.net/lizhitao/article/details/39499283 估计大神会不定期更新,所以还是访问这个链接看最新的目录list比较好 apa ...
- sqldatasource控件设置where语句
按照学号查找显示信息,我现在也不知道各部分代表的含义,先记下来
- 安装Kali Linux操作系统Kali Linux无线网络渗透
安装Kali Linux操作系统Kali Linux无线网络渗透 Kali Linux是一个基于Debian的Linux发行版,它的前身是BackTrack Linux发行版.在该操作系统中,自带了大 ...
- 递推DP URAL 1260 Nudnik Photographer
题目传送门 /* 递推DP: dp[i] 表示放i的方案数,最后累加前n-2的数字的方案数 */ #include <cstdio> #include <algorithm> ...
- BZOJ3322 : [Scoi2013]摩托车交易
求出最大生成树,则两点间的最大容量为树上两点间的边权的最小值. 设$lim[i]$表示第$i$个订单的城市允许携带的黄金上限,则 $lim[i]=\min(lim[i+1],a[i]和a[i+1]点间 ...
- BZOJ3578 : GTY的人类基因组计划2
关于如何判断一个集合是否出现过: 给每个元素随机一个hash权值,然后xor起来即可 插入删除都只需xor 线段树维护区间有效人数和,以及打标记表示这个区间的集合要全部标记为出现过,并把区间内sum值 ...
- javascript生成n至m的随机整数
摘要: 本文讲解如何使用js生成n到m间的随机数字,主要目的是为后期的js生成验证码做准备. Math.random()函数返回0和1之间的伪随机数,可能为0,但总是小于1,[0,1) 生成n-m,包 ...
- 用C语言实现素数筛法获取一亿(100000000)以内的全部素数
具体筛法是:先把n个自然数按次序排列起来.1不是质数,也不是合数,要划去.第二个数2是质数留下来,而把2后面所有能被2整除的数都划去.2后面第一个没划去的数是3,把3留下,再把3后面所有能被3整除的数 ...
- 【MVC框架整合】之 SpringMVC3.2.0+MyBatis3.1.1+Spring3.2.0
1.先整合spring和Mybatis 第一步基本上都是一样加入jar包 创建测试目录 添加junit jar包和log4j配置文件 Log4j的配置文件基本上都是不会变的复制过来就行了 现在就和Hi ...
- java+easyui实例
1.首先引入easyui包 在jsp页面上引用以下文件: <link rel="stylesheet" type="text/css" href=&quo ...