簡體   English   中英

遍歷集合和.Add

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM