如何将这些“var”结果分配给不同的变量?

时间:2017-08-17 14:45:29

标签: c# model-view-controller var

这些是我的结果:

enter image description here

嘿大家。

我希望将这些结果分配给不同的变量。

例如;

stringone = "Etiler";
stringtwo = "Kağıthane";
stringthree = "Şişli";

1 个答案:

答案 0 :(得分:-1)

C#是强类型的变量必须在编译时定义,因此您无法在运行时动态创建变量。 但是,您可以使用集合来保存结果。

使用列表:

var result = db.servis.Where(s => ...).ToList();
// access first element:
var first = result.ElementAt(0);

使用数组:

var result = db.servis.Where(s => ...).ToArray();
// access first element:
var first = result[0]; // here "0" is the array index

使用字典:

var result = db.servis.Where(s => ...)
    .Select((item, index) => new {index, item})
    .ToDictionary(x => "string" + (x.index + 1), x => x.item);
// access first element:
var first = result["string1"]; // here "string1" is the key of the key value pair
相关问题