[英]Convert from Query postgresql to linq C#
I want work with linq using "groupby" with "string_agg" but i can't find reference from this query.我想使用带有“string_agg”的“groupby”与 linq 一起工作,但我无法从此查询中找到参考。
SELECT DISTINCT "TB_M_GROUP_EMP"."GROUP_ID" AS "ID",
string_agg(Distinct "TB_M_EMPLOYEES"."EMP_NAME",',' order by "TB_M_EMPLOYEES"."EMP_NAME" ) as "Employee Name"
FROM public."TB_M_GROUP_EMP", "TB_M_EMPLOYEES"
GROUP BY "TB_M_GROUP_EMP"."GROUP_ID";
or this code或此代码
SELECT DISTINCT "TB_M_GROUP_EMP"."GROUP_ID" as "Group Id", array_agg(Distinct"TB_M_EMPLOYEES"."EMP_CODE" || ',' ||"TB_M_EMPLOYEES"."EMP_NAME"|| ',' ||"TB_M_EMPLOYEES"."EMP_EMAIL" ) as "Employees"
FROM public."TB_M_GROUP_EMP", "TB_M_EMPLOYEES"
GROUP BY "TB_M_GROUP_EMP"."GROUP_ID";
please help to convert this query into linq C# and give a reference or documentation linq!请帮助将此查询转换为 linq C# 并提供参考或文档 linq!
I have tried to use this code but it's not quite what I want the output to be.我曾尝试使用此代码,但这并不是我想要的输出。
groupEmployee
.GroupBy(e => new { e.GroupId, e.Employee }, (key, group) => new
{
Employee = key.Employee,
Employees = group.ToList()
})
Example table :示例表:
--------------
TB_M_EMPLOYEES
--------------
|EMP_ID|EMP_CODE|EMP_NAME|EMP_EMAIL|
------------------------------------
|1 |E0012 |Ali |@ali |
|2 |E0013 |AL |@al |
|3 |E0014 |Abi |@abi |
|4 |E0015 |Ad |@ad |
|5 |E0016 |Ea |@ea |
----------------------
TB_M_GROUP_EMP
----------------------
|Id|GROUP_ID|EMP_ID|
----------------------
|1 |1 |1 |
|2 |1 |2 |
|3 |1 |3 |
|4 |1 |4 |
|5 |1 |5 |
from two table in above.从上面的两个表。 i want the output to be the table this bottom.
我希望输出是这个底部的表格。
TB_M_GROUP_EMP
-----------------
|GROUP_ID|EMP|
-----------------
|1 |{"E0012,Ali,@ali","E0013,Al,@al","E0014,Abi,@abi","E0015,Ad,@ad","E0016,Ea,@ea"}|
Thanks.谢谢。
Check out:查看:
https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.aggregate?view=netcore-3.1 https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.aggregate?view=netcore-3.1
Ex.前任。
items.Aggregate((i, j) => i + delimiter + j));
This likely won't produce the same SQL statement - so you may have to optimise for performance.这可能不会产生相同的 SQL 语句 - 因此您可能必须优化性能。
Maybe you can type this using LINQ:也许您可以使用 LINQ 键入:
var result =
from employee in groupEmployee
group employee by employee.GroupId into output
select output;
And now you have your output in result.现在你有你的输出结果。 For answering your question I used the following article: https://docs.microsoft.com/en-gb/dotnet/csharp/linq/group-query-results
为了回答您的问题,我使用了以下文章: https : //docs.microsoft.com/en-gb/dotnet/csharp/linq/group-query-results
Assuming, we have the following models:假设我们有以下模型:
public class Employee
{
public int EmployeeId { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
public class EmployeeGroup
{
public int Id { get; set; }
public int GroupId { get; set; }
public int EmployeeId { get; set; }
}
Try, the below:试试看,如下:
var result = groups
.Join(employees, grp => grp.EmployeeId, emp => emp.EmployeeId, (group, employee) =>
new
{
group.GroupId,
employee.EmployeeId,
employee.Code,
employee.Name,
employee.Email,
})
.GroupBy(res => res.GroupId)
.Select(res => new
{
GroupId = res.Key,
Names = string.Join(",", res
.OrderBy(e => e.Name)
.Select(e => $"{e.Code},{e.Name},{e.Email}"))
})
.ToList();
Yet, another verson here could be (maybe more close to the output you want):然而,这里的另一个版本可能是(可能更接近您想要的输出):
var result = groups.GroupJoin(employees, grp => grp.EmployeeId, emp => emp.EmployeeId,
(grp, emp) => new
{
grp.GroupId,
Names = string.Join(",", emp.Select(e => $"{e.Code},{e.Name},{e.Email}"))
}).
GroupBy(e=> e.GroupId)
.ToList();
If you want the ordered (by name) version for the above, then the query can become a bit more complex:如果您想要上面的有序(按名称)版本,那么查询可能会变得更复杂一些:
var result = groups
.GroupJoin(employees,
grp => grp.EmployeeId,
emp => emp.EmployeeId,
(grp, emps) =>
{
var empsList = emps.ToList();
return new
{
grp.GroupId,
NamesCmp = empsList.Select(e => e.Name).FirstOrDefault(), // this needed for Ordering
Names = string.Join(",", empsList.Select(e => $"{e.Code},{e.Name},{e.Email}"))
};
})
.GroupBy(e => e.GroupId)
.Select(grp => new
{
GroupId = grp.Key,
Items = grp.OrderBy(g => g.NamesCmp)
.Select(g => g.Names)
})
.ToList();
Please note that using real class models for the above instead of anonymous types is more preferable.请注意,对上述使用真实类模型而不是匿名类型更可取。
Based on my understanding you are looking for below solution.根据我的理解,您正在寻找以下解决方案。 If I missed something then please let me know.
如果我错过了什么,请告诉我。
Below is the code:下面是代码:
var result = (from empRow in TB_M_EMPLOYEES.AsEnumerable()
join grpRow in TB_M_GROUP_EMP.AsEnumerable()
on empRow.Field<string>("EMP_ID") equals grpRow.Field<string>("EMP_ID")
select new
{
EMP_ID = empRow.Field<string>("EMP_ID"),
EMP_CODE = empRow.Field<string>("EMP_CODE"),
EMP_NAME = empRow.Field<string>("EMP_NAME"),
EMP_EMAIL = empRow.Field<string>("EMP_EMAIL"),
GROUP_ID = grpRow.Field<string>("GROUP_ID")
}).GroupBy(r => r.GROUP_ID)
.Select(rs => new
{
GroupId = rs.Key,
FinalNames = string.Join(",", rs
.Select(e => $"\"{e.EMP_CODE},{e.EMP_NAME},{e.EMP_EMAIL}\""))
});
Test Function is as below:测试功能如下:
static void PostResSql()
{
DataTable TB_M_EMPLOYEES = new DataTable();
TB_M_EMPLOYEES.Columns.Add("EMP_ID");
TB_M_EMPLOYEES.Columns.Add("EMP_CODE");
TB_M_EMPLOYEES.Columns.Add("EMP_NAME");
TB_M_EMPLOYEES.Columns.Add("EMP_EMAIL");
DataTable TB_M_GROUP_EMP = new DataTable();
TB_M_GROUP_EMP.Columns.Add("Id");
TB_M_GROUP_EMP.Columns.Add("GROUP_ID");
TB_M_GROUP_EMP.Columns.Add("EMP_ID");
DataRow row = TB_M_EMPLOYEES.NewRow();
row[0] = "1";
row[1] = "E0012";
row[2] = "Ali";
row[3] = "@ali";
TB_M_EMPLOYEES.Rows.Add(row);
row = TB_M_EMPLOYEES.NewRow();
row[0] = "2";
row[1] = "E0013";
row[2] = "AL";
row[3] = "@al";
TB_M_EMPLOYEES.Rows.Add(row);
row = TB_M_EMPLOYEES.NewRow();
row[0] = "3";
row[1] = "E0014";
row[2] = "Abi";
row[3] = "@abi";
TB_M_EMPLOYEES.Rows.Add(row);
row = TB_M_EMPLOYEES.NewRow();
row[0] = "4";
row[1] = "E0015";
row[2] = "Ad";
row[3] = "@ad";
TB_M_EMPLOYEES.Rows.Add(row);
row = TB_M_EMPLOYEES.NewRow();
row[0] = "5";
row[1] = "E0016";
row[2] = "Ea";
row[3] = "@ea";
TB_M_EMPLOYEES.Rows.Add(row);
row = TB_M_GROUP_EMP.NewRow();
row[0] = "1";
row[1] = "1";
row[2] = "1";
TB_M_GROUP_EMP.Rows.Add(row);
row = TB_M_GROUP_EMP.NewRow();
row[0] = "2";
row[1] = "1";
row[2] = "2";
TB_M_GROUP_EMP.Rows.Add(row);
row = TB_M_GROUP_EMP.NewRow();
row[0] = "3";
row[1] = "1";
row[2] = "3";
TB_M_GROUP_EMP.Rows.Add(row);
row = TB_M_GROUP_EMP.NewRow();
row[0] = "4";
row[1] = "1";
row[2] = "4";
TB_M_GROUP_EMP.Rows.Add(row);
row = TB_M_GROUP_EMP.NewRow();
row[0] = "5";
row[1] = "1";
row[2] = "5";
TB_M_GROUP_EMP.Rows.Add(row);
var result = (from empRow in TB_M_EMPLOYEES.AsEnumerable()
join grpRow in TB_M_GROUP_EMP.AsEnumerable()
on empRow.Field<string>("EMP_ID") equals grpRow.Field<string>("EMP_ID")
select new
{
EMP_ID = empRow.Field<string>("EMP_ID"),
EMP_CODE = empRow.Field<string>("EMP_CODE"),
EMP_NAME = empRow.Field<string>("EMP_NAME"),
EMP_EMAIL = empRow.Field<string>("EMP_EMAIL"),
GROUP_ID = grpRow.Field<string>("GROUP_ID")
}).GroupBy(r => r.GROUP_ID)
.Select(rs => new
{
GroupId = rs.Key,
FinalNames = string.Join(",", rs
.Select(e => $"\"{e.EMP_CODE},{e.EMP_NAME},{e.EMP_EMAIL}\""))
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.