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 ...
随机推荐
- JavaScript中toStirng()与Object.prototype.toString.call()方法浅谈
toStirng()与Object.prototype.toString.call()方法浅谈 一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种 ...
- 【wikioi】1913 数字梯形问题(费用流)
http://wikioi.com/problem/1913/ 如果本题没有询问2和3,那么本题和蚯蚓那题一模一样.http://www.cnblogs.com/iwtwiioi/p/3935039. ...
- Memcached、Redis OR Tair
一.前言 非关系型数据库(NoSQL = Not Only SQL)的产品非常多,常见的有Memcached.Redis.MongoDB等优秀开源项目,相关概念和资料网上也非常丰富,不再重复描述,本文 ...
- Java/Js下使用正则表达式匹配嵌套Html标签
转自:http://www.jb51.net/article/24422.htm 以前写过一篇文章讲解如何使用正则表达式完美解决Html嵌套标签的匹配问题(使用正则表达式匹配嵌套Html标签),但是里 ...
- C# params object[] args 可以传多个参数,可以不限制类型(转)
C# params object[] args 可以传多个参数,可以不限制类型 using System;using System.Collections.Generic;using System.T ...
- C#时间格式化(Datetime)用法详解
Datetime.ToString(String, IFormatProvider) 参数format格式详细用法: 格式字符 关联属性/说明 d ShortDatePattern D LongDat ...
- CSS对浏览器的兼容性(IE和Firefox)技巧整理
CSS对浏览器的兼容性有时让人很头疼,或许当你了解当中的技巧跟原理,就会觉得也不是难事,从网上收集了IE7,6与Fireofx的兼容性处理技巧并整理了一下.对于web2.0的过度,请尽量用xhtml格 ...
- Add SSH Key to GitLab on Windows
Download Git for windows Open Git Bash Type in "ssh-keygen -t rsa", and then press Enter b ...
- java第一节课
1.安装 2.编写java程序 首先,新建一个文本文档:把后缀改成.java,然后起一个文件名,要是英文的,如:Hello. 然后,编辑,代码如下: class Hello { public stat ...
- ImageMagick jmagick 安装
在安装ImageMagick之前,请检查下面包已经安装 tiff-3.9.5.tar.gz (rpm -qa|grep libtiff检查是否已经安装) libpng-1.2.46.t ...