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 ...
随机推荐
- 大牛之路II
时间限制:500MS 内存限制:1000K 提交次数:138 通过次数:31 题型: 编程题 语言: C++;C Description 要成为ACM大牛,要掌握很多必需的知识点.某些知识点可以 ...
- stack UVA 442 Matrix Chain Multiplication
题目传送门 题意:给出每个矩阵的行列,计算矩阵的表达式,如果错误输出error,否则输出答案 分析:表达式求值,stack 容器的应用:矩阵的表达式求值A 矩阵是a * b,B 矩阵是b * c,则A ...
- Linq to Sql/entity Join
inner join 模板: var query = from x in db.T1 join y in db.T2 on x.Id equals ...
- HDU3657 Game(最小割)
题目大概说,给一个n×m的格子,每个格子都有数字,选择一个格子就能加上格子数字的分数,有k个格子必须选择,如果两个相邻的格子都被选择了那分数要减去两个格子数字的与再乘2.问能取得的最大分数. 已经知道 ...
- 整除的尾数[HDU2099]
整除的尾数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- Travel Problem[SZU_K28]
DescriptionAfter SzuHope take part in the 36th ACMICPC Asia Chendu Reginal Contest. Then go to QingC ...
- ! cocos2d 同一个sprite的触控问题
如果对一个A sprite添加触控,然后在一个场景中创建四个A的实例,那么1234逐个添加的话,只有最后一个会被点击到.其他的将不会响应.
- BZOJ2051 : A Problem For Fun
树的点分治,将点分治的过程记录下来,每一个分治结构按到分治中心的距离维护所有点. 对于一个点二分答案,然后在$O(\log n)$个分治结构中二分查找,时间复杂度$O(n\log^3n)$. #inc ...
- TYVJ P1002 谁拿了最多奖学金 Label:模拟 水
背景 NOIP2005复赛提高组第一题 描述 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同:1) 院士奖学金,每人8000元,期末平均成绩高于80分( ...
- Memcached、Redis OR Tair
一.前言 非关系型数据库(NoSQL = Not Only SQL)的产品非常多,常见的有Memcached.Redis.MongoDB等优秀开源项目,相关概念和资料网上也非常丰富,不再重复描述,本文 ...