2010-10-27 36 views
4

我有類屬性的區域,我想添加數據,我用linq讀取這個屬性。linq to C#中的列表問題#

例如

List<Zones> z = new List<Zones> 
z.add(new Zones(...)); 

var allZones = from s in db.Zones select s; 

我怎樣才能增加allZones到Z泛型列表?

回答

5

你可以做到這一點在許多方面:

z.AddRange(allZones); // if there are other elements in z 
z = allZones.ToList(); // if there are no other elements in z (creates a new list) 
2
allZones.ForEach(x => z.Add(x)); 

z.AddRange(allZones.ToList()); 
2

如果allZones是IEnumerable的<區>中您可以使用

z.AddRange(allZones) 
2
var z = db.Zones.ToList(); 

然後將所有新區域添加到列表中。

z.AddRange(db.Zones); 
1

z=db.select(X=>X.Zones).ToList()