linq to List problem in C#
I have class Zones with properties and i want add data which i read with linq to this properties.
example
List<Zones> z = new 开发者_运维技巧List<Zones>
z.add(new Zones(...));
var allZones = from s in db.Zones select s;
How can i add allZones to z generic List?
You can do it in a number of ways:
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)
allZones.ForEach(x => z.Add(x));
or
z.AddRange(allZones.ToList());
If allZones is IEnumerable<Zones> the you can use
z.AddRange(allZones)
z.AddRange(allZones.ToList())
var z = db.Zones.ToList();
Then add any new zones to the list.
or
z.AddRange(db.Zones);
z=db.select(X=>X.Zones).ToList()
精彩评论