gpt4 book ai didi

c# - 将表行解析为嵌套集合

转载 作者:行者123 更新时间:2023-11-30 15:32:34 24 4
gpt4 key购买 nike

我有一个如下所示的数据表。我正在尝试使用 C# 使用 list 2 中的嵌套集合类结构对它们进行分组,以便结果类似于列表 3。每个级别上的每个集合都不应该有重复值。有谁知道如何解析表格?谢谢。

list 1:

    user_ID firstName Role                      service                     facility        RWP      Fac_ID     svc_ID   rol_ID
------- --------- ----------------------- --------------------------- -------------- -----
874 Joe Tech Primary New York, NY 9-2 1 1 1
874 Joe Reviewer Primary New York, NY 2-5 1 1 2
874 Joe Reviewer Primary Los Angeles, CA 2-5 2 1 2
874 Joe Super User Primary Los Angeles, CA 9-10 2 1 3
874 Joe Administrator Application Administration Los Angeles, CA 1-2 2 2 4
874 Joe Super User Application Administration Chicago, IL 10-12 3 2 3

list 2:

Facility {
fac Facility;
List<Service> Services;
}
Service{
svc Service;
List<Role> Roles;
}
Role{
rol Role;
List<RWP> RWP;
}
RWP{
string H;
}
fac{
int fac_ID;
string fac_Name;
}
svc{
int svc_ID;
string svc_Name;
}
rol{
int rol_ID;
string rol_Name;
}

list 3:

                 NY
|
+-- Primary --+
| |
Tech Reviewer
| |
9-2 2-5

+------- LA --------------------+
| |
Application +----Primary----+
Administration | |
| | |
Administrator Reviewer Super User
| | |
1-2 2-5 9-10

CH
|
Application
Administration
|
Super User
|
10-12

list 4:

private static void populateUserFSR(ref User thisUser, DataTable dt, string FSRtype)
{
string tmpFacility = string.Empty;
string tmpService = string.Empty;
string tmpRole = string.Empty;
FSR thisFSR = new FSR();
thisFSR.serviceRoles = new List<SR>();

SR thisSR = new SR();
thisSR.service = new List<Service>();
thisSR.rolesWatchProviders = new List<RP>();

RP thisRWP = new RP();
thisRWP.providers = new List<string>();

foreach (DataRow r in dt.Rows)
{
if (r["facility_ID"].ToString().Trim().ToUpper() != tmpFacility)
{
//new facility row
if (!string.IsNullOrEmpty(tmpFacility))
{
//add to a proper collection according to their FSRtype
switch (FSRtype)
{
case "Assigned":
thisUser.FSRAssigned.Add(thisFSR);
break;
case "Preferred":
thisUser.FSRPreferred.Add(thisFSR);
break;
case "Selected":
thisUser.FSRSelected.Add(thisFSR);
break;
}
thisFSR = new FSR();
thisFSR.serviceRoles = new List<SR>();

thisSR = new SR();
thisSR.service = new List<Service>();
thisSR.rolesWatchProviders = new List<RP>();

thisRWP = new RP();
thisRWP.providers = new List<string>();
}
tmpFacility = r["facility_ID"].ToString();
tmpService = string.Empty;
tmpRole = string.Empty;
thisFSR.facility = new Facility();
thisFSR.facility.Facility_ID = int.Parse(r["facility_ID"].ToString());
thisFSR.facility.Facility_Name = r["facility_name"].ToString();

//only rank with FSR Assigned, no ranking with FSR Preferred
if (FSRtype == "Assigned")
thisFSR.rank = int.Parse(r["rank"].ToString());
}

if (r["Service_ID"].ToString().Trim().ToUpper() != tmpService)
{
tmpService = r["Service_ID"].ToString();
tmpRole = string.Empty;
Service thisService = new Service();
thisService.ServiceID = int.Parse(r["Service_ID"].ToString());
thisService.ServiceName = r["fac_service"].ToString();
thisSR.service = new List<Service>();
thisSR.service.Add(thisService);
thisFSR.serviceRoles.Add(thisSR);
}

if (r["Role"].ToString().Trim().ToUpper() != tmpRole)
{
tmpRole = r["Role"].ToString();
thisRWP.role = new Role();
thisRWP.role.Id = int.Parse(r["role_ID"].ToString());
thisRWP.role.Role = r["Role"].ToString();
thisRWP.providers = getCohortPCP(thisUser);
thisSR.rolesWatchProviders.Add(thisRWP);
}
}
}

最佳答案

试试这个:

var list3 = list1.AsEnumerable()
.GroupBy(x=>x.Field<string>("facility"))
.Select(g=> new Facility{ Facility=g.Key,
Services = g.GroupBy(x=>x.Field<string>("Role"))
.Select(g1=> new Service{
Service = g1.Key,
Roles = g1.GroupBy(x=>x.Field<string>("RWP"))
.Select(g2=> new Role{
H = g2.Key
}).ToList()
}).ToList()
}).ToList();

关于c# - 将表行解析为嵌套集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18839473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com