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语句一样,用法简单,功能强大. 最近需要实现一个从两个不同的文件读取不同的数据,然后根据这两个数 ...
随机推荐
- SQL Server数据库备份(本机)
基础的SQL Server数据库备份存储过程 /**************************************************************************** ...
- 一个Makefile
CC = g++ CCFLAGS = -O3 -DNDEBUG INC = -I ../../include SRC = $(wildcard *.cpp) OBJ = $(patsubst %.cp ...
- 简单的SQL Server在线查看和执行系统
在大的公司里面,往往数据库和表都非常的多,一张表的字段也是会有几十个.如果这么多的表和字段没有说明注释的话,查看起来会一头雾水,新来的或者别的部门的同事看到表和字段会完全不知道是干嘛的,只能靠名称来猜 ...
- LocalStorage 本地存储
首先自然是检测浏览器是否支持本地存储.在HTML5中,本地存储是一个window的属性,包括localStorage和sessionStorage,从名字应该可以很清楚的辨认二者的区别,前者是一直存在 ...
- uva 11461
简单 打个表 case数不超过200 数据比较水 木有超时的风险~~ /*************************************************************** ...
- ural 1066 uva 1555
好吧 竟然因为编译器的问题不过 到底有什么区别 ???? 可以推出公式Hi = (i-1)H2 +(i-1)(i-2)-(i-2)*H1 因为所有的Hi都要大于零 Hn要最小 即存在Hi=0 ...
- linux下执行 ls,cat等一些命令报出 -bash: /bin/cat: Cannot allocate memory 有没解决的方法
环境变量配置出错了cd -- 进入用户目录vim .bash_profile删除以前PATH这一行,把下面的粘帖进去PATH=$PATH:$HOME/bin:/root:/root/snapshot/ ...
- 用 OneAPM Cloud Insight 监控 Docker 性能
Docker 是构建和部署软件的一个新兴的轻量级的平台,也是一个减轻替代虚拟机的容器.Docker 通过给开发者提供兼容不同环境的镜像,成为解决现代基础设施的持续交付的一个流行的解决方案. 和虚拟机一 ...
- python List&Set&Dict交集、并集、差集
1.python List交集.并集.差集 1). 获取两个list 的交集#方法一: a=[2,3,4,5] b=[2,5,8] tmp = [val for val in a if val in ...
- VS2005中SetUnhandledExceptionFilter函数应用
很多软件通过设置自己的异常捕获函数,捕获未处理的异常,生成报告或者日志(例如生成mini-dump文件),达到Release版本下追踪Bug的目的.但是,到了VS2005(即VC8),Microsof ...