[英]How to iterate through two collections of the same length using a single foreach
[英]Iterate through collections and .Add
我正在嘗試在循環外存儲數據的foreach
循環集合以供以后使用。
考慮以下foreach
循環。
IList<DetailViewModel> storeAllLoopItems = new List<DetailViewModel>();
foreach (var item in _listOfItems)
{
var items =
(from a in context.Employee
join b in context.Orders on a.Id equals b.Id
join d in context.Notes on a.Id equals d.Id
where a.Id == item.id
select new DetailViewModel
{
Name = b.LegalName,
Abbrev = c.Abbrev,
TaxAmount = a.Amount,
}).ToList();
storeAllLoopItems.Add(items); //<<<ERROR
}
錯誤:
'System.Collections.Generic.List.Add(DetailViewModel)'的最佳重載方法匹配具有一些無效的參數
無法從“ System.Collections.Generic.List”轉換為“ DetailViewModel”
Add
方法用於添加單個值。 您將需要手動將項目添加到列表中,因為IList
接口沒有AddRange
方法。 或將定義更改為具有AddRange
方法的List<DetailValueModel>
。
另外, .ToList()
並且浪費內存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.