LINQ,EF联合查询join
- public object GetListAdmin()
- {
- //return db_C56.Admins
- // .Where(a => a.Status != "D").ToList();
- var query1 = db_C56.Admins.Join(db_C56.Area, a => a.AreaID, ar => ar.ID, (a, ar) => new
- {
- userName = a.UserName,
- pwd = a.Password,
- dName = a.DisplayName,
- areaId = a.AreaID,
- hasNode = a.HasNode,
- roleName = a.RoleName,
- status = a.Status,
- areaName = ar.Name
- });
- var query = from a in db_C56.Admins
- join ar in db_C56.Area
- on a.AreaID equals ar.ID
- where a.Status != "D"
- select new
- {
- userName = a.UserName,
- pwd = a.Password,
- dName = a.DisplayName,
- areaId = a.AreaID,
- hasNode = a.HasNode,
- roleName = a.RoleName,
- status = a.Status,
- areaName = ar.Name
- };
- return query.ToList().Select(C => new Admin
- {
- UserName = C.userName,
- Password = C.pwd,
- DisplayName = C.dName,
- AreaID = C.areaId,
- AreaPath = C.areaName,
- HasNode = C.hasNode,
- RoleName = C.roleName,
- Status = C.status,
- });
- }
- from v in Pdt_Versions
- join t in Tb_TypeDics
- on v.TypeName equals t.TypeName into ignored
- from i in ignored.DefaultIfEmpty()
- where v.Status != "D"
- select new
- {
- ID = v.ID,
- VersionName = v.VersionName,
- VersionCode = v.VersionCode,
- DownloadName = v.DownloadName,
- DownloadURL = v.DownloadURL,
- VType = v.VType,
- TypeName = v.TypeName,
- DisplyTypeName = i.DisplyTypeName,
- }
Linq 多层嵌套查询
- var query1 = from p in dbContent.PostService
- where p.post_type == "product" &&
- (from ot1 in dbContent.OrderItemmetaService
- where
- (from ot2 in dbContent.OrderItemsService
- where ot2.order_item_type == "line_item" &&
- (from p1 in dbContent.PostService
- where p1.post_type == "shop_order" && p1.post_author == userid && p1.post_status == "wc-completed"
- select p1.ID).Contains(ot2.order_id)
- select ot2.order_item_id).Contains(ot1.meta_id)
- select ot1.meta_value).Contains(p.ID)
- select new
- {
- id = p.ID,
- name = p.post_title
- };
- var query2 = dbContent.PostService.Where(p =>
- p.post_type == "product" &&
- (dbContent.OrderItemmetaService.Where(ot1 =>
- (dbContent.OrderItemsService.Where(ot2 =>
- ot2.order_item_type == "line_item" && (dbContent.PostService.Where(p1 =>
- p1.post_type == "shop_order" && p1.post_author == userid && p1.post_status == "wc-completed").Select(p1 => p1.ID).Contains(ot2.order_item_id))
- ).Select(ot2 => ot2.order_item_id).Contains(ot1.meta_id))
- ).Select(ot1 => ot1.meta_value).Contains(p.ID))
- ).Select(p => new
- {
- id = p.ID,
- name = p.post_title
- }).ToList();
Left Join 查询
from d in Doctors
join c in (
(from t in Commentaries where t.State != 'D' group t by new { t.DoctorID } into g
select new {
DoctorID = (Int64?)g.Key.DoctorID,
Total = (Int32?)g.Sum(p => p.Rating),
Evaluate = (System.Double?)g.Average(p => p.Rating)
})) on new { UserID = d.UserID } equals new { UserID = (Int64)c.DoctorID }into a_join
from p in a_join.DefaultIfEmpty()
select new {
d.ID,
UserID = (Int64?)d.UserID,
d.Name,
Evaluate = ((int?)p.Evaluate ?? (int?)0)
}
Lambda表达式
Doctors
.GroupJoin (
Commentaries
.Where (t => ((Int32)(t.State) != 68))
.GroupBy (
t =>
new
{
DoctorID = t.DoctorID
}
)
.Select (
g =>
new
{
DoctorID = (Int64?)(g.Key.DoctorID),
Total = (Int32?)(g.Sum (p => p.Rating)),
Evaluate = (Double?)(g.Average (p => p.Rating))
}
),
d =>
new
{
UserID = d.UserID
},
c =>
new
{
UserID = (Int64)(c.DoctorID)
},
(d, a_join) =>
new
{
d = d,
a_join = a_join
}
)
.SelectMany (
temp0 => temp0.a_join.DefaultIfEmpty (),
(temp0, p) =>
new
{
ID = temp0.d.ID,
UserID = (Int64?)(temp0.d.UserID),
Name = temp0.d.Name,
Evaluate = ((Int32?)(p.Evaluate) ?? (Int32?)0)
}
)
======================================================================
多个left join
from d in Doctors
join f in Functions on new { FunctionID = d.FunctionID } equals new { FunctionID = f.ID } into b_join
from f in b_join.DefaultIfEmpty()
join c in (
(from t in Commentaries where t.State != 'D' group t by new {t.DoctorID } into g
select new {
DoctorID = (Int64?)g.Key.DoctorID,
Total = (Int32?)g.Sum(p => p.Rating),
Evaluate = (System.Double?)g.Average(p => p.Rating)
})) on new { UserID = d.UserID } equals new { UserID = (Int64)c.DoctorID } into a_join
from c in a_join.DefaultIfEmpty()
select new {
d.ID,
UserID = (Int64?)d.UserID,
d.AvatarPic,
d.Name,
f.Title,
f.ContentDescribe,
Evaluate = ((int?)c.Evaluate ?? (int?)0)
}
Lambda表达式
.GroupJoin (
Functions,
d =>
new
{
FunctionID = d.FunctionID
},
f =>
new
{
FunctionID = f.ID
},
(d, b_join) =>
new
{
d = d,
b_join = b_join
}
)
.SelectMany (
temp0 => temp0.b_join.DefaultIfEmpty (),
(temp0, f) =>
new
{
temp0 = temp0,
f = f
}
)
.GroupJoin (
Commentaries
.Where (t => ((Int32)(t.State) != 68))
.GroupBy (
t =>
new
{
DoctorID = t.DoctorID
}
)
.Select (
g =>
new
{
DoctorID = (Int64?)(g.Key.DoctorID),
Total = (Int32?)(g.Sum (p => p.Rating)),
Evaluate = (Double?)(g.Average (p => p.Rating))
}
),
temp1 =>
new
{
UserID = temp1.temp0.d.UserID
},
c =>
new
{
UserID = (Int64)(c.DoctorID)
},
(temp1, a_join) =>
new
{
temp1 = temp1,
a_join = a_join
}
)
.SelectMany (
temp2 => temp2.a_join.DefaultIfEmpty (),
(temp2, c) =>
new
{
ID = temp2.temp1.temp0.d.ID,
UserID = (Int64?)(temp2.temp1.temp0.d.UserID),
AvatarPic = temp2.temp1.temp0.d.AvatarPic,
Name = temp2.temp1.temp0.d.Name,
Title = temp2.temp1.f.Title,
ContentDescribe = temp2.temp1.f.ContentDescribe,
Evaluate = ((Int32?)(c.Evaluate) ?? (Int32?)0)
}
)
LINQ,EF联合查询join的更多相关文章
- EF联合查询的新用法
用EF很多年了,做联合查询时,只知道linq和lambda两种语法,今天朋友发了一个链接,打开看后发现是EF内置的新的关于联合查询的方法,赶紧抄录下来,以备后用. 现在先把这几种方法,各写一个例子,便 ...
- 【HIVE】(3)联合查询join、时间戳函数、字符串函数
数据 t_join1.txt 1,a,1 2,b,2 3,c,4 t_join2.txt 1,2a 2,2b 3,2c 建表.导入: create table t_join1(id int, name ...
- EF 表联合查询 join
有两张表m_Dept.m_User,联合查询 linq方式.EF方式 private void Add() { List<m_Dept> lst = new List<m_Dept& ...
- EF联合查询,如何设置条件过滤从表数据
最近在使用EF进行联合查询过程中,遇到了一件不开心的事情. 已禁用懒加载 var post = await _repository.GetMyPostById(blogId, postId).AsNo ...
- EF 联合查询
EF 文章表和标签表联合查询标签id在dis中的文章,还不知道性能如何 var query = tagRepo.Entities.Include("Tags").Where(t = ...
- 转:EntityFramework查询--联合查询(Join,GroupJoin)
首先我们先看一下Join public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this ...
- EntityFramework查询--联合查询(Join,GroupJoin)
首先我们先看一下Join public static IEnumerable<TResult> Join<TOuter, TInner, TKey, TResult>(this ...
- sqlalchemy多表联合查询(join)
使用outerjoin instances = db.session.query(Instance.name, Instance.sep_status, User.email).outerjoin( ...
- linq中如何实现多个条件的联合查询
目前接触处理数据这一块比较多,在处理内存中的数据源的时候我一般使用的是linq,linq使用起来像sql语句一样,用法简单,功能强大. 最近需要实现一个从两个不同的文件读取不同的数据,然后根据这两个数 ...
随机推荐
- oracle闪回表详解
--- 说明闪回数据库 --- 使用闪回表将表内容还原到过去的特定时间点 --- 从删除表中进行恢复 --- 使用闪回查询查看截止到任一时间点的数据库内容 --- 使用闪回版本查询查看某一行在一段时间 ...
- iOS Icon尺寸、iPhone Ratina 分辨率
高清晰度的iPhone和iPod touch(单位:像素) 启动影像 :640 x 960 APP图标:114 x 114 App Store商店:1024 x 1024 Spotlight搜索小图标 ...
- 微软职位内部推荐-SENIOR PRODUCER
微软近期Open的职位: Role Based in Shanghai, ChinaTitle: ProducerWe are seeking a Senior Producer to lead Pr ...
- javascript面向对象思想2
上篇说到面向对象可以帮我们梳理页面的逻辑的文章(http://www.cnblogs.com/hetaojs/p/6024013.html),很多朋友看了说我这种写法是初级的面向对象小儿科,确实是初级 ...
- github简单使用
github是一个基于git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开.对于一般人来说公共仓库就已经足够了,而且我们也没多少代码来管理,O(∩_∩)O ...
- C# Path
http://hi.baidu.com/mayijun0410/item/4fa6c6c154a1c35ebdef69f9 using System.IO; Path类的静态方法: ChangeExt ...
- PAT-乙级-1020. 月饼 (25)
1020. 月饼 (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 月饼是中国人在中秋佳节时吃的一种传统食 ...
- Eclipse SVN插件冲突导致不能使用解决办法
最近,由于安装插件导致eclipse的SVN插件不能使用,出现的问题实在很烦恼,通过试验发现当新安装的插件安装完毕后,只需要把eclipse-jee-kepler-SR2-win32-x86_64/e ...
- 图形学:图像围绕着某个点P(a,b)旋转------白话版
前提:在研究图形时候,我们并没有规定图形的大小,所以任意图形多是支持的,这也另外说明了一点,图形转换和图形的大小没有关系. 如果图像围绕着某个点P(a,b)旋转,则先要将坐标系平移到该点,再进行旋转, ...
- [itint5]直角路线遍历棋盘
http://www.itint5.com/oj/#22 这题一开始直接用暴力的DFS来做,果然到25的规模就挂了. vector<bool> visited(50, false); ve ...