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中的一些方法的总结的更多相关文章

  1. Lambda 中如果构建一个查询条件,扔该Where返回我们需要的数据。

    有一个需求,比如所 省市县 这三个查询条件 都可能有可能没有,但是我们的查询条件怎么构建呢 首先需要看一下 Lambda中Where这个方法需要什么参数 public static IEnumerab ...

  2. C#中的匿名方法

    C#中的匿名方法是在C#2.0引入的,它终结了C#2.0之前版本声明委托的唯一方法是使用命名方法的时代.虽然在 C# 3.0 及更高版本中,Lambda 表达式取代了匿名方法,作为编写内联代码的首选方 ...

  3. 简谈 JavaScript、Java 中链式方法调用大致实现原理

    相信,在 JavaScript .C# 中都见过不少链式方法调用,那么,其中实现该类链式调用原理,大家有没有仔细思考过?其中 JavaScript 类库:jQuery 中就存在大量例子,而在 C# 中 ...

  4. Lambda表达式与匿名方法

    在C#2中,由于有了方法组,匿名方法,类型的协变和抗变,使得运用delegate变得很容易,在注册事件时代码变得简单易读,但是在C# 2中,代码仍然有点臃肿,大块的匿名方法会降低代码的可读性,一般我们 ...

  5. Java 8新特性探究(一) JEP126特性lambda表达式和默认方法

    Lambda语法 函数式接口 函数式接口(functional interface 也叫功能性接口,其实是同一个东西).简单来说,函数式接口是只包含一个方法的接口.比如Java标准库中的java.la ...

  6. java代码之美(10)---Java8 Map中的computeIfAbsent方法

    Map中的computeIfAbsent方法 Map接口的实现类如HashMap,ConcurrentHashMap,HashTable等继承了此方法,通过此方法可以在特定需求下,让你的代码更加简洁. ...

  7. [二] java8 函数式接口详解 函数接口详解 lambda表达式 匿名函数 方法引用使用含义 函数式接口实例 如何定义函数式接口

    函数式接口详细定义 package java.lang; import java.lang.annotation.*; /** * An informative annotation type use ...

  8. 委托学习过程及委托、Lambda表达式和匿名方法的关系总结及事件总结

    第一章,当开始学习委托的时候,我们会问什么是委托?为什么要学习委托? 一,什么是委托? 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法, ...

  9. Java 8 接口中的默认方法与静态方法

    Java 8 接口中的默认方法与静态方法 1. 接口中的默认方法 允许接口中包含具有具体实现的方法,该方法称"默认方法",默认方法使用用 default 关键字修饰. public ...

随机推荐

  1. servlet中cookie的使用

    ---恢复内容开始--- Cookie是存储在客户端计算机上的文本文件,并保留了它们的各种信息跟踪的目的. Java Servlet透明支持HTTP Cookie. 涉及标识返回用户有三个步骤: 服务 ...

  2. sql 截取字符串与 截取字符串最长的字符串

    ); set @str='aa,32,22,55,7'; ) as '第一个逗号的索引值' )),),),'') as '第一个值' ),len(@str)) as '从第一逗号开始截取出后面的字符串 ...

  3. Spring的bean标签

    Spring框架中主要有四种标签bean.alias.import.beans,其中bean标签是其他标签的基础. 一.bean标签的属性 scope:用来配置spring bean的作用域 sing ...

  4. jquery概要--基础02

    复制节点:clone();默认不会复制绑定事件,如果传入参数true会复制:替换节点: replaceWith()              //原节点放在前,新节点放在在后: replaceAll( ...

  5. Linux基础命令(1)

    使用Ctr + Alt + F1(2,3,4,5,6)进入终端. 使用Ctr + Alt + F7回到界面. date 显示系统日期 cal 2015 显示2015年的日历表 reboot 重启 sh ...

  6. BZOJ3931 [CQOI2015]网络吞吐量(最大流)

    没啥好说的,有写过类似的,就是预处理出最短路上的边建容量网络. #include<cstdio> #include<cstring> #include<queue> ...

  7. POJ1061 青蛙的约会(线性同余方程)

    线性同余方程$ ax \equiv b \pmod n$可以用扩展欧几里得算法求解. 这一题假设青蛙们跳t次后相遇,则可列方程: $$ Mt+X \equiv Nt+Y \pmod L$$ $$ (M ...

  8. POJ2125 Destroying The Graph(二分图最小点权覆盖集)

    最小点权覆盖就是,对于有点权的有向图,选出权值和最少的点的集合覆盖所有的边. 解二分图最小点权覆盖集可以用最小割: vs-X-Y-vt这样连边,vs和X部点的连边容量为X部点的权值,Y部和vt连边容量 ...

  9. cocos 帧率测试

    有人说导致cocos2dx 帧率下降的是getPosition,我测试以后发现并不是这样的. local MainScene = class("MainScene", functi ...

  10. BZOJ2874 : 训练士兵

    设$a[i][j]$表示$(i,j)$右下角要增加多少 $aj[i][j]=a[i][j]\times j$ $ai[i][j]=a[i][j]\times i$ $aij[i][j]=a[i][j] ...