[Solved] How can i assign these “var” results to different variables? [closed]


C# is strongly typed variables must be defined at compile time, so you can not create variables dynamically at runtime.
However, you can use a collection to hold your results.

Using a list:

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

Using an array:

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

Using a dictionary:

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

3

solved How can i assign these “var” results to different variables? [closed]