core的 Linq基本使用,简单模拟数据库多表的左右内连接的测试
1:先看效果:
using System;
using System.Collections.Generic;
using System.Linq;
namespace Redistest02
{
class Program
{
static void Main(string[] args)
{
//Console.WriteLine("Hello DoSubscriberAsync!");
//MyRedisHelper.DoSubscriberAsync("myredis").Wait(30);
//Console.ReadLine();
//==================准备模拟两张表的数据
var list01 = new List<Student>();//3条数据
Enumerable.Range(1, 3).ToList().ForEach(c =>
{
list01.Add(new Student { id = 11 + c, Name = "qq" + c });
});
var list02 = new List<Student>();//两条数据
list02.Add(new Student { id = 12, Name = "qq1" });
list02.Add(new Student { id = 13, Name = "qq2" });
Console.WriteLine("==========左连接==以左边为准=============");
//左连接
var newlistL = (from q in list01
join a in list02
on q.id equals a.id into qa
from c in qa.DefaultIfEmpty()
select new Student
{
id = c == null ? 0 : c.id,
Name = c == null ? "空的" : c.Name
}).ToList();
newlistL.ForEach(c => Console.WriteLine($"id={c.id},name={c.Name}"));
//右连接
Console.WriteLine("==========右连接===以右边为准============");
var newlistR = (from a in list02
join q in list01
on a.id equals q.id into qa
from c in qa.DefaultIfEmpty()
select new Student
{
id = c == null ? 0 : c.id,
Name = c == null ? "空的" : c.Name
}).ToList();
newlistR.ForEach(c => Console.WriteLine($"id={c.id},name={c.Name}"));
//内连接
Console.WriteLine("==========内连接======两边共同的数据=========");
var newlistI = (from a in list02
join q in list01
on a.id equals q.id
select new Student
{
id = q == null ? 0 : q.id,
Name = q == null ? "空的" : q.Name
}).ToList();
newlistI.ForEach(c => Console.WriteLine($"id={c.id},name={c.Name}"));
Enumerable.Range(1, 10).ToList().ForEach(c =>
{
Console.WriteLine(c);
});
var listdata = Enumerable.Empty<Student>();
Console.WriteLine($"listdata的集合对象数据是:{listdata.Count()}个,null就会报错的!");
}
}
public class Student
{
public int id { get; set; }
public string Name { get; set; }
// public DateTime Birthday { get; set; }
//public IEnumerable<Student> GetSpectionMenthod
//{
// get
// {
// // yield return Enumerable.Empty<Student>().FirstOrDefault();
// yield return new Student { };
// }
}
}
View Code
怎么样,看了之后还是很简单的对吧,嘻嘻!
- ASP.net core 2.0.0 中 asp.net identity 2.0.0 的基本使用(一)—修改数据库连接
- php中mysql连接和基本操作代码(快速测试使用,简单方便)
- 如何在postgresql中模拟oracle的dual表,来测试数据库最基本的连接功能?
- PHP5中PDO的简单使用 连接数据库
- MyEclipse使用Java 通过JDBC连接MySQL数据库的基本测试
- SpringBoot使用Driud连接数据库(仅配置测试连接,通过配置类方法配置)
- 【all】 如何测试本机是否能连接到数据库的最简单的方法
- mysql的安装与简单使用以及myeclipse下与数据库的连接和使用
- JDBC第一篇--【介绍JDBC、使用JDBC连接数据库、简单的工具类】
- JDBC——(从复杂到简单)连接使用数据库的多种实现方法
- 使用microsoft EnterpriseLibrary连接不同数据库简单应用 .
- PHP面向对象的使用教程 简单数据库连接
- 连接池技术与传统数据库连接的比较和简单使用
- 简单测试数据库连接
- sqlplus的基本使用例子(关闭并重启数据库)和连接一个数据库
- IDEA使用JDBC连接数据库,简单实例
- PHP面向对象的使用教程 简单数据库连接
- 使用microsoft EnterpriseLibrary连接不同数据库简单应用
- java application 应用程序 使用JDBC proxool 两种方式连接数据库 测试程序代码
- Objective-C ,ios,iphone开发基础:使用第三方库FMDB连接sqlite3 数据库,实现简单的登录