c# - 使用 Lambda C# 使用另一个列表更新对象列表中的属性
问题描述
我想更新对象列表中的属性。该属性来自 aspnetUsers 列表
这是对象
Object
{
public Guid Id { get; set; }
public string profesionalId{ get; set; }
public string attributeToUpdate { get; set; }
}
我有这段代码不起作用
List<Object> MyListOfObject=new List<Object>();
// do some queries to populate the list
// more things here...
//my first problem comes here, can not get the list of users
var listOfUsers= _dbContext.Users.Where(u => MyListOfObject.Any(id => id.ProfesionalId == u.Id)).ToList();
如果上面的代码有效,我想做一个
MyListOfObject.ForEach(
//here replace the MyListOfObject.attributeToUpdate
).where (MyListOfObject.profesionalId==listOfUsers.Id)
请问我可以得到解决方案吗?我尽力做到这一点,但无法解决。谢谢
解决方案
建立列表:
var listOfUsers= _dbContext.Users.Where(u => MyListOfObject.Select(c=>c.ProfesionalId).ToList().Contains(u.id)).ToList();
但总的来说,您应该可以这样做:
MyListOfObject.ForEach(l => l.PropertyToUpdate= _dbContext.Users.First(x => x.id == l.ProfessionalId).PropertyYouGetDataFrom);
推荐阅读
- excel - 引用公式中的单元格地址/引用
- angular - Angular 自动完成功能似乎无法正常工作
- python - 在 django 中启动 celery task_monitor
- python - 如何停止/终止运行 python 脚本?(再次)
- python - Python:将大型数据框(1.2M 行)插入 Postgres 的问题
- java - 2.1.6 Spring Boot - Elasticsearch Healthcheck 失败
- angular - 使用 mqtt-ngx 通过 websocket 在 tls 中连接到代理
- r - 与微软团队的 slackr_upload 等效的 R 函数
- sql - 数字格式 SQL INSERT
- c# - 如果 5 分钟内没有记录任何内容,如何使进程崩溃