EF to linq 左连接

如果连接的数据不存在用 null 表示,则可以左连接查询,但是如果数据类型为 int 则会出错。

var ng = (from g in _db.NET_NEWS_GROUP
join z in _db.NET_NEWS_GROUP_INFO on g.NET_NEWS_GROUP_ID equals z.NET_NEWS_GROUP_ID
into nglist
from n in nglist.DefaultIfEmpty()
select new
{
NET_NEWS_GROUP_ID = n.NET_NEWS_GROUP_ID,
NET_NEWS_ID = n.NET_NEWS_ID,
GROUP_NAME = n.NET_NEWS_GROUP.GROUP_NAME
}).ToList();

// 左连接查询 必须有主外间关系。

var ng = _db.NET_NEWS_GROUP.Include("NET_NEWS_GROUP_INFO")

如果没有主外键关系,又是INT 型 怎么办? 。。。

// 如果没有主外键关系,又是INT 型 怎么办, 想查询一下 外键的数量。

var ng = (from g in _db.NET_NEWS_GROUP select g).ToList();
var zl = (from g in _db.NET_NEWS_GROUP_INFO select g).ToList();

foreach (var item in ng)
{
item.INSERT_ID = zl.Where(z => z.NET_NEWS_GROUP_ID == item.NET_NEWS_GROUP_ID).Count().ToString();
}

时间: 2024-10-13 22:18:33

EF to linq 左连接的相关文章

EF的表左连接方法Include和Join

在EF中表连接常用的有Join()和Include(),两者都可以实现两张表的连接,但又有所不同. 例如有个唱片表Album(AlbumId,Name,CreateDate,GenreId),表中含外键GenreId连接流派表Genre(GenreId,Name).每个唱片归属唯一一个流派,一个流派可以对应多个唱片. 1.Join(),两表不必含有外键关系,需要代码手动指定连接外键相等(具有可拓展性,除了值相等,还能指定是>,<以及其他对两表的相应键的关系),以及结果字段. 重载方式(是扩展方

Linq 左连接 left join

Suppose you have a tblRoom and tblUserInfo. Now, you need to select all the rooms regardless of whether the room has user information or not. This calls for a LEFT JOIN which will select everything from the LEFT side (the room side) regardless of the

linq 左连接

var list = (from item in vall join item3 in v1 on new { item.FItemID, item.FAuxPropID } equals new { item3.FItemID, item3.FAuxPropID } into stockqty from itemstock in stockqty.DefaultIfEmpty() select new ICStockBillEntry { FAuxPropID = item.FAuxPropI

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_

Linq 和 SQL的左连接、右连接、内链接

在我们工作中表连接是很常用的,但常用的有这三种连接方式:左连接.右连接.内链接 在本章节中讲的是1.如何在Linq中使用左连接,右连接,内连接. 2.三种连接之间的特点在哪? 3.Linq的三种连接语法是怎么样的呢(我觉得左右连接也就相当换个位置) 一.SQL Server 中的三种连接 首先我们示范以下SQL中的左连接,右连接,内连接,需要准备两张表: CREATE TABLE [dbo].[Company]( [Id] int identity Primary key, [Code] uni

LINQ的左连接、右连接、内连接

1.左连接: var LeftJoin = from emp in ListOfEmployees join dept in ListOfDepartment on emp.DeptID equals dept.ID into JoinedEmpDept from dept in JoinedEmpDept.DefaultIfEmpty() select new { EmployeeName = emp.Name, DepartmentName = dept != null ? dept.Nam

Linq 实现左连接,右连接

左连接: var LeftJoin = from emp in ListOfEmployees join dept in ListOfDepartment on emp.DeptID equals dept.ID into JoinedEmpDept from dept in JoinedEmpDept.DefaultIfEmpty() select new { EmployeeName = emp.Name, DepartmentName = dept != null ? dept.Name

LINQ的左连接、右连接、内连接和Lamda表达式实现Left join

1.左连接: var LeftJoin = from t1 in l1join t2 in l2on t1.ID equals t2.ID into Joinedt12from t3 in Joinedt12.DefaultIfEmpty()select new                        {Name = t1.Name,Age=t2.Age                      };   2.右连接: var RightJoin = from t2 in l2join t

Linq表连接大全(INNER JOIN、LEFT OUTER JOIN、RIGHT OUTER JOIN、FULL OUTER JOIN、CROSS JOIN)

转载http://www.cnblogs.com/shenqiboy/p/3260105.html 我们知道在SQL中一共有五种JOIN操作:INNER JOIN.LEFT OUTER JOIN.RIGHT OUTER JOIN.FULL OUTER JOIN.CROSS JOIN        内连接.Sql: SELECT [t0].[GroupName], [t1].[UserName] FROM [Group] AS [t0] INNER JOIN [User] AS [t1] ON (