EF Lambda 多表查询

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcForLamadaToTableJion.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
LamadaJoinEvent();

return View();
}

/// <summary>
/// 实体类
/// </summary>
public class Product
{

public int ProductId { get; set; }

public string ProductName { get; set; }
}

#region 填充 数据
static Product[] product2 =
{
new Product{ProductId=1,ProductName="Apple"},
new Product{ProductId=4,ProductName="Orange"},
new Product{ProductId=5,ProductName="Berry"},
};
static Product[] product3 =
{

new Product{ProductId=4,ProductName="Orange"},
new Product{ProductId=5,ProductName="Berry"},
};
#endregion

#region 调用lamada多表查询方法
/// <summary>
/// 调用lamada多表查询方法,
/// 说明:这种方法目前比较适合两张表连接,三张表以上估计够呛
/// </summary>
public void LamadaJoinEvent()
{

var result3 = product2.Join
(
product3,
x => x.ProductId,
c => c.ProductId,
(x, c) => new { x.ProductId, c.ProductName }
);

foreach (var a in result3)
{
ViewBag.data +="||" +a.ProductName;
}

}
#endregion

public ActionResult About()
{
ViewBag.Message = "你的应用程序说明页。";

return View();
}

public ActionResult Contact()
{
ViewBag.Message = "你的联系方式页。";

return View();
}

}
}

时间: 2024-08-24 05:31:23

EF Lambda 多表查询的相关文章

EF的连表查询Lambda表达式

var lst = from c in db.Blogs join p in db.Posts on c.Id equals p.BlogId where p.Id==1 select c; var lst1 = db.Blogs.Join(db.Posts.Where(p=>p.Id==1), b=> b.Id, p=> p.BlogId, (b, p) => new {b}); public class Blog { public int Id { get; set; } pu

EF的连表查询Lambda表达式和linq语句(转)

var lst = from c in db.Blogs join p in db.Posts on c.Id equals p.BlogId where p.Id==1 select c; var lst1 = db.Blogs.Join(db.Posts.Where(p=>p.Id==1), b=> b.Id, p=> p.BlogId, (b, p) => new {b}); public class Blog { public int Id { get; set; } pu

使用Entity Framework Core需要注意的一个全表查询问题

.NET Core 迁移工作如火如荼,今天在使用 Entity Frameowork Core(又名EF Core)时写了下面这样的 LINQ 查询表达式: .Where(u => u.Id == new Guid(userId)).FirstOrDefaultAsync(); 结果在 SQL Server Profiler 中发现竟然进行了全表查询. 之后将 new Guid(userId) 从表达式中移出,保存于一个局部变量中,使用这个局部变量进行查询,全表查询问题就解决了. var use

Linq多表查询 返回组合实体 踩坑记

新年就不再记流水账了吧,无关紧要的日志太多也不好,有时要找自已记录的一些要点要翻半天 春节假期在家陆陆续续也有在做公司的事,主要是重构,接手的代码看着比较乱,花了很多时间来重构,现在看上去好多了. 使用Linq进行多表查询,要返回各个表的几个组合数据, public class A{ public string NameA{get; set;} } public class B{ public string NameB{get; set;} } 1.原先项目是定义了一个类,里面包含几张表的各个字

EF的左连接查询

在EF中,当在dbset使用join关联多表查询时,连接查询的表如果没有建立相应的外键关系时,EF生成的SQL语句是inner join(内联),对于inner join,有所了解的同学都知道,很多时候这并不是我们的本意,实例如下: var list = from o in context.CTMS_OD_ORDERS join d in context.CTMS_SUP_DOCTOR on o.OWNERDOCID equals d.USERID join e in context.CTMS_

一个MySQL 单表查询SQL,引起一次“故事”

一.描述故事的过程是这个样子的,MySQL 数据库有一张40多G的大表,开发人员执行了一个单表查询,结果我就收到了一个磁盘空间满的告警.一下子就不淡定了,谁在搞事情,脑海里各种可能行想了一遍,想到的最多的就是ibtmp1爆了,第一时间线上看一下,查到了最大的那个文件,结果和想的一样.这里我们要看SQL是怎么写的,表结构是什么样子,然后和官网对比,MySQL在查询的时候有很多可能会使用磁盘临时表,包括表连接.排序.大字段等等. 二.解决方法临时解决方法:1.临时扩一下硬盘2.直接重启 终结解决方案

Mysql数据库理论基础之五--SELECT单多表查询、子查询、别名

一.简介 由MySQL AB公司开发,是最流行的开放源码SQL数据库管理系统,主要特点: 1.是一种数据库管理系统 2.是一种关联数据库管理系统 3.是一种开放源码软件,且有大量可用的共享MySQL软件 4.MySQL数据库服务器具有快速.可靠和易于使用的特点 5.MySQL服务器工作在客户端/服务器模式下,或嵌入式系统中 InnoDB存储引擎将InnoDB表保存在一个表空间内,该表空间可由数个文件创建.这样,表的大小就能超过单独文件的最大容量.表空间可包括原始磁盘分区,从而使得很大的表成为可能

web day16 数据库 完整性约束,mySQL编码问题,备份与恢复,多表查询

约束 * 约束是添加在列上的,用来约束列的! 1. 主键约束(唯一标识) ****非空*** ****唯一*** ****被引用**** * 当表的某一列被指定为主键后,该列就不能为空,不能有重复值出现. * 创建表时指定主键的两种方式: > CREATE TABLE stu( sid      CHAR(6) PRIMARY KEY, sname     VARCHAR(20), age         INT, gender     VARCHAR(10) ); 指定sid列为主键列,即为s

python3 mysql 多表查询

python3 mysql 多表查询 一.准备表 创建二张表: company.employee company.department #建表 create table department( id int, name varchar(20) ); create table employee( id int primary key auto_increment, name varchar(20), sex enum('male','female') not null default 'male'