我列出了人员的姓名和名字,以及人员姓名和姓氏的清单.有些人没有名字,有些人没有姓氏; 我想在两个列表上进行完全外连接.
所以以下列表:
ID FirstName
-- ---------
1 John
2 Sue
ID LastName
-- --------
1 Doe
3 Smith
Run Code Online (Sandbox Code Playgroud)
应该产生:
ID FirstName LastName
-- --------- --------
1 John Doe
2 Sue
3 Smith
Run Code Online (Sandbox Code Playgroud)
我是LINQ的新手(如果我是跛脚的话,请原谅我)并找到了很多"LINQ Outer Joins"的解决方案,这些解决方案看起来非常相似,但实际上似乎是留下了外部联接.
到目前为止,我的尝试是这样的:
private void OuterJoinTest()
{
List<FirstName> firstNames = new List<FirstName>();
firstNames.Add(new FirstName { ID = 1, Name = "John" });
firstNames.Add(new FirstName { ID = 2, Name = "Sue" });
List<LastName> lastNames = new List<LastName>();
lastNames.Add(new LastName { ID = 1, Name = "Doe" …Run Code Online (Sandbox Code Playgroud) 我正在尝试LINQ实体.
我有以下问题:我希望它这样做:
SELECT
T_Benutzer.BE_User
,T_Benutzer_Benutzergruppen.BEBG_BE
FROM T_Benutzer
LEFT JOIN T_Benutzer_Benutzergruppen
ON T_Benutzer_Benutzergruppen.BEBG_BE = T_Benutzer.BE_ID
Run Code Online (Sandbox Code Playgroud)
我最接近的是这个:
var lol = (
from u in Repo.T_Benutzer
//where u.BE_ID == 1
from o in Repo.T_Benutzer_Benutzergruppen.DefaultIfEmpty()
// on u.BE_ID equals o.BEBG_BE
where (u.BE_ID == o.BEBG_BE || o.BEBG_BE == null)
//join bg in Repo.T_Benutzergruppen.DefaultIfEmpty()
// on o.BEBG_BG equals bg.ID
//where bg.ID == 899
orderby
u.BE_Name ascending
//, bg.Name descending
//select u
select new
{
u.BE_User
,o.BEBG_BG
//, bg.Name
}
).ToList();
Run Code Online (Sandbox Code Playgroud)
但是这会产生与内部联接相同的结果,而不是左联接.
而且,它创建了这个完全疯狂的SQL:
SELECT
[Extent1].[BE_ID] AS [BE_ID] …Run Code Online (Sandbox Code Playgroud) c# linq linq-to-entities entity-framework entity-framework-4
SELECT
e.EmpName,
me.RemarkNumber,
me.RemarkPeopleNumber
FROM
EmployeeInfo e
LEFT JOIN (
SELECT
COUNT(RemarkId) As RemarkNumber,
COUNT(DISTINCT MemberId) As RemarkPeopleNumber,
CreateUser
FROM
MemberRemark
WHERE
RemarkStatus = 0
GROUP BY
CreateUser
) AS me On e.EmpName = me.CreateUser
WHERE
BranchCode = '0000'
And [Status] = 0
Run Code Online (Sandbox Code Playgroud)
如何将其转换为 LINQ?
from e in db.EmployeeInfo
join me in (
from memberRemarks in db.MemberRemark
where memberRemarks.RemarkStatus == 0
group memberRemarks by new
{
memberRemarks.CreateUser,
} into g
select new
{
RemarkNumber = g.Count(),
RemarkPeopleNumber = g.Select(m => …Run Code Online (Sandbox Code Playgroud) 我有一个用于替换数据库函数的 Linq 查询。这是第一个有多个连接的,我似乎无法弄清楚为什么它返回 0 结果。
如果您能看到任何可能导致错误返回的差异,将不胜感激......我一直在尝试解决它的时间比我应该有的时间更长。
Linq 查询
context.StorageAreaRacks
.Join(context.StorageAreas, sar => sar.StorageAreaId, sa => sa.Id, (sar, sa) => new { sar, sa })
.Join(context.StorageAreaTypes, xsar => xsar.sar.StorageAreaId, sat => sat.Id, (xsar, sat) => new { xsar, sat })
.Join(context.Racks, xxsar => xxsar.xsar.sar.RackId, r => r.Id, (xxsar, r) => new { xxsar, r })
.Where(x => x.xxsar.sat.IsManual == false)
.Where(x => x.r.IsEnabled == true)
.Where(x => x.r.IsVirtual == false)
.Select(x => new { x.xxsar.sat.Id, x.xxsar.sat.Name })
.Distinct()
.ToList();
Run Code Online (Sandbox Code Playgroud)
这是由 LINQ …
我想在 EF Core 中创建一个查询,如下所示:
SELECT SUM(DATEDIFF(DAY, FromDate, ToDate)) AS Diff
FROM Table1
Run Code Online (Sandbox Code Playgroud)
上述查询适用于 T-SQL - 如何在 Linq 中创建查询?