C#:根据公共标识符合并两个对象?
C#: Merge two objects based on common identifier?
我有一个 POCO class,其中嵌套 classes 定义如下:
class Company
{
string CompanyName { get; set; }
List<Server> Servers { get; set; }
}
class Server
{
int ServerId { get; set;}
string ServerName { get; set; }
List<User> Users { get; set; }
}
class User
{
string FirstName { get; set; }
string LastName { get; set; }
}
假设我用以下数据实例化了它:
CompanyName = "Company A";
Servers =
ServerId = 134;
ServerName = HOST01;
Users =
FirstName = "John";
LastName = "Smith";
FirstName = "Bob";
LastName = "Jones";
ServerId = 232;
ServerName = HOST31;
Users =
FirstName = "Michael";
LastName = "Stevens";
ServerId = 134;
ServerName = HOST01;
Users =
FirstName = "Larry";
LastName = "Edwards";
FirstName = "Craig";
LastName = "White";
我如何根据匹配 "ServerId" 属性 将所有用户合并在一起?基本上,我怎么能把它变成这个?
CompanyName = "Company A";
Servers =
ServerId = 134;
ServerName = HOST01;
Users =
FirstName = "John";
LastName = "Smith";
FirstName = "Bob";
LastName = "Jones";
FirstName = "Larry";
LastName = "Edwards";
FirstName = "Craig";
LastName = "White";
ServerId = 232;
ServerName = HOST31;
Users =
FirstName = "Michael";
LastName = "Stevens";
使用 LINQ GroupBy
在 ServerId
上分组,并假设具有相同 ID 的服务器也具有相同的名称。
var grouped = Companies // IEnumerable<Company>
.Select(c => new Company
{
CompanyName = c.CompanyName,
Servers = c.Servers
.GroupBy(s => s.ServerId)
.Select(grp => new Server
{
ServerId = grp.Key,
ServerName = grp.First().ServerName,
Users = grp.SelectMany(s => s.Users)).ToList()
})
});
你可以使用Linq
,像下面的代码:
1 - 通过 ID 和名称获取不同的服务器:
List<Server> distinctServers = company.Servers
.GroupBy(s => new { s.ServerId, s.ServerName })
.Select(s => new Server
{
ServerId = s.Key.ServerId,
ServerName = s.Key.ServerName,
Users = s.SelectMany(x => x.Users).ToList()
}).ToList();
2 - 建立新公司或使用旧公司:
Company result = new Company
{
CompanyName = company.CompanyName,
Servers = distinctServers
};
// or
company.Servers = distinctServers;
如果您有公司列表,您可以在 select
函数中使用相同的代码:
List<Company> companies = new List<Company> { company };
List<Company> companiesWithDistinctServers = companies
.Select(c => new Company
{
CompanyName = c.CompanyName,
Servers = c.Servers.GroupBy(s => new { s.ServerId, s.ServerName })
.Select(s => new Server
{
ServerId = s.Key.ServerId,
ServerName = s.Key.ServerName,
Users = s.SelectMany(x => x.Users).ToList()
}).ToList()
}).ToList();
希望对您有所帮助
我有一个 POCO class,其中嵌套 classes 定义如下:
class Company
{
string CompanyName { get; set; }
List<Server> Servers { get; set; }
}
class Server
{
int ServerId { get; set;}
string ServerName { get; set; }
List<User> Users { get; set; }
}
class User
{
string FirstName { get; set; }
string LastName { get; set; }
}
假设我用以下数据实例化了它:
CompanyName = "Company A";
Servers =
ServerId = 134;
ServerName = HOST01;
Users =
FirstName = "John";
LastName = "Smith";
FirstName = "Bob";
LastName = "Jones";
ServerId = 232;
ServerName = HOST31;
Users =
FirstName = "Michael";
LastName = "Stevens";
ServerId = 134;
ServerName = HOST01;
Users =
FirstName = "Larry";
LastName = "Edwards";
FirstName = "Craig";
LastName = "White";
我如何根据匹配 "ServerId" 属性 将所有用户合并在一起?基本上,我怎么能把它变成这个?
CompanyName = "Company A";
Servers =
ServerId = 134;
ServerName = HOST01;
Users =
FirstName = "John";
LastName = "Smith";
FirstName = "Bob";
LastName = "Jones";
FirstName = "Larry";
LastName = "Edwards";
FirstName = "Craig";
LastName = "White";
ServerId = 232;
ServerName = HOST31;
Users =
FirstName = "Michael";
LastName = "Stevens";
使用 LINQ GroupBy
在 ServerId
上分组,并假设具有相同 ID 的服务器也具有相同的名称。
var grouped = Companies // IEnumerable<Company>
.Select(c => new Company
{
CompanyName = c.CompanyName,
Servers = c.Servers
.GroupBy(s => s.ServerId)
.Select(grp => new Server
{
ServerId = grp.Key,
ServerName = grp.First().ServerName,
Users = grp.SelectMany(s => s.Users)).ToList()
})
});
你可以使用Linq
,像下面的代码:
1 - 通过 ID 和名称获取不同的服务器:
List<Server> distinctServers = company.Servers
.GroupBy(s => new { s.ServerId, s.ServerName })
.Select(s => new Server
{
ServerId = s.Key.ServerId,
ServerName = s.Key.ServerName,
Users = s.SelectMany(x => x.Users).ToList()
}).ToList();
2 - 建立新公司或使用旧公司:
Company result = new Company
{
CompanyName = company.CompanyName,
Servers = distinctServers
};
// or
company.Servers = distinctServers;
如果您有公司列表,您可以在 select
函数中使用相同的代码:
List<Company> companies = new List<Company> { company };
List<Company> companiesWithDistinctServers = companies
.Select(c => new Company
{
CompanyName = c.CompanyName,
Servers = c.Servers.GroupBy(s => new { s.ServerId, s.ServerName })
.Select(s => new Server
{
ServerId = s.Key.ServerId,
ServerName = s.Key.ServerName,
Users = s.SelectMany(x => x.Users).ToList()
}).ToList()
}).ToList();
希望对您有所帮助