I have the following code:
public IList<Content.Grid> GetContentGrid(string pk)
{
// How can I define result to hold the return
// data? I tried the following but it does not
// work:
var result = new IList<Content.Grid>();
var data = _contentRepository.GetPk(pk)
.Select((t, index) => new Content.Grid()
{
PartitionKey = t.PartitionKey
....
});
switch (pk.Substring(2, 2))
{
case "00":
return data
.OrderBy(item => item.Order)
.ToList();
break;
default:
return data
.OrderBy(item => item.Order)
.ToList();
break;
}
}
The VS2012 is telling me that the break is not needed so what I would like to do is to remove the returns from inside the switch, store the results in a variable and then after the switch is completed have:
return result;
Can someone tell me how I can declare the variable called result. I tried the following but this gives a syntax error:
var result = new IList<Content.Grid>();
You already return the result in your switch:
There’s no need to declare a variable before/return it after the switch, because you jump out of the switch with the return-statement. (That’s why you don’t need the break)
However, you could use the following:
…
…