[英]foreach in foreach list c#
The GalleryDetail.Id
is 148
when it enters the first foreach
loop, and the GalleryDetail.Id
is 148
when it enters the second foreach
loop. GalleryDetail.Id
在进入第一个foreach
循环时为148
, GalleryDetail.Id
在进入第二个foreach
循环时为148
。 But it does not enter the first foreach
loop again.但它不会再次进入第一个foreach
循环。 It continues from the second.它从第二个继续。 How do I get it to re-enter the first loop here?我如何让它重新进入这里的第一个循环?
NOTE: I do not have direct access from the GalleryDetail.Id
request.注意:我没有从GalleryDetail.Id
请求的直接访问权限。
var detailList = await _repo.GalleryDetail.GetAllAsync();
foreach (var item in mapped.GalleryDetails)
{
foreach (var item2 in detailList)
{
if (item.Id != item2.Id)
{
var mapped3 = _mapper.Map<GalleryDetails>(item2);
await _repo.GalleryDetail.DeleteAsync(mapped3);
}
}
}
It's not necessary to use 2 loop here, you can use LinQ instead.这里不需要使用 2 循环,您可以使用 LinQ 代替。 And you should not leave async Delete inside foreach loop, because it will connect to you database mutiple time.而且您不应该将异步删除留在 foreach 循环中,因为它会多次连接到您的数据库。 Example:例子:
var detailList = await _repo.GalleryDetail.GetAllAsync();
//Map the whole list
var detailListMapped = _mapper.Map<List<GalleryDetails>>(item2);
//Use LinQ to find database items not in request
var deletedList = detailList.Where(x => !mapped.GalleryDetails.Any(y => y.Id == x.Id)).ToList();
//Entity Framework have RemoveRange function, you should use it here
await _repo.GalleryDetail.RemoveRangeAsync(deletedList );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.