will never get hit because they're after the break statement. You should see a warning about "Unreachable code detected".
Mark all posts that give the desired result the answer. If you only mark the last that gave you clarification because you misread an earlier post others will be confused. Some of us are here to help others and our point to post ratio matters.
Here my A, B , C.. is also a list of string. I couldn't use char variable c cast to string list c.
<table class="ExpertiseTable">
<tr>
@for (char c = 'A'; c < 'Z'; c++)
{
<th>
<a href="#@c">@c</a>
</th>
}
</tr>
@{
List<string> A = new List<string>();
List<string> B = new List<string>();
List<string> C = new List<string>();
List<string> D = new List<string>();
List<string> E = new List<string>();
List<string> F = new List<string>();
List<string> G = new List<string>();
List<string> H = new List<string>();
List<string> I = new List<string>();
List<string> J = new List<string>();
List<string> K = new List<string>();
List<string> L = new List<string>();
List<string> M = new List<string>();
List<string> N = new List<string>();
List<string> O = new List<string>();
List<string> P = new List<string>();
List<string> Q = new List<string>();
List<string> R = new List<string>();
List<string> S = new List<string>();
List<string> T = new List<string>();
List<string> U = new List<string>();
List<string> V = new List<string>();
List<string> W = new List<string>();
List<string> X = new List<string>();
List<string> Y = new List<string>();
List<string> Z = new List<string>();
Dictionary<char, List<string>> names = new Dictionary<char, List<string>>();
}
@foreach (var item in ViewBag.Expertises)
{
switch (char.ToUpper(item[0]))
{
case 'A':
A.Add(item);
break;
case 'B':
B.Add(item);
break;
case 'C':
C.Add(item);
break;
case 'D':
D.Add(item);
break;
case 'E':
E.Add(item);
break;
case 'F':
F.Add(item);
break;
case 'G':
G.Add(item);
break;
case 'H':
H.Add(item);
break;
case 'I':
I.Add(item);
break;
case 'J':
J.Add(item);
break;
case 'K':
K.Add(item);
break;
case 'L':
L.Add(item);
break;
case 'S':
S.Add(item);
break;
case 'T':
T.Add(item);
break;
case 'P':
P.Add(item);
break;
}
}
@for (char c = 'A'; c < 'Z'; c++)
{
switch (c)
{
case 'A':
@Html.Raw("<td> <a name='A'></a><ul>")
foreach (var exp in A)
{
@Html.Raw("<li>") @Html.ActionLink(@exp, "SearchResults", new { Keywords = @exp }) @Html.Raw("</li>")
}
@Html.Raw("</ul></td>")
break;
case 'B':
@Html.Raw("<td> <a name='B'></a><ul>")
foreach (var exp in B)
{
@Html.Raw("<li>") @Html.ActionLink(@exp, "SearchResults", new { Keywords = @exp }) @Html.Raw("</li>")
}
@Html.Raw("</ul></td>")
break;
case 'C':
@Html.Raw("<td> <a name='C'></a><ul>")
foreach (var exp in C)
{
@Html.Raw("<li>") @Html.ActionLink(@exp, "SearchResults", new { Keywords = @exp }) @Html.Raw("</li>")
}
@Html.Raw("</ul></td>")
break;
...
}
@Html.Raw("</tr>")
}
</table>
IMHO, still overly complicated. Sort the results in the controller action not the View. As far as I can tell there is no reason for the switch as a simple loop will do fine as long as you format the View Model in the controller first.
The question you meet may be how to find the list by local variables :
foreach (var exp in A)
You can use a data structure that's designed for it - in this case, a Dictionary<string, List<string>>. This will allow you to set a name as a key to access an object:
List<string> A = new List<string>();
List<string> B = new List<string>();
List<string> C = new List<string>();
List<string> D = new List<string>();
List<string> E = new List<string>();
List<string> F = new List<string>();
Dictionary<string, List<string>> lists = new Dictionary<string, List<string>>();
lists.Add("A", A);
lists.Add("B", B);
lists.Add("C", C);
lists.Add("D", D);
........
Then you can access a specific list by name through the Dictionary's indexer property:
var cList = lists["A"];
Then your loop will like something as :
@for ()
{
.....
var cList = lists[c.ToString()];
foreach (var exp in cList )
{
@Html.Raw("<li>") @Html.ActionLink(@exp, "SearchResults", new {
Keywords = @exp }) @Html.Raw("</li>")
}
.....
}
But i would suggest re-design your logic , put the complicated logic on server side .
Member
65 Points
250 Posts
Variable in Switch-Case in Razor?
Sep 21, 2018 02:24 PM|jj819|LINK
Hi, I have A-Z switch case loop. Just wondering if the following code can be simplified? Got error message that case has to be a constant.
All-Star
52171 Points
23259 Posts
Re: Variable in Switch-Case in Razor?
Sep 21, 2018 02:32 PM|mgebhard|LINK
You've over complicated the design and do not need a switch at all just a loop since you know the char value.
Contributor
7048 Points
2187 Posts
Re: Variable in Switch-Case in Razor?
Sep 21, 2018 02:32 PM|ryanbesko|LINK
One issue I see is that these lines:
will never get hit because they're after the break statement. You should see a warning about "Unreachable code detected".
Member
65 Points
250 Posts
Re: Variable in Switch-Case in Razor?
Sep 21, 2018 03:03 PM|jj819|LINK
Here my A, B , C.. is also a list of string. I couldn't use char variable c cast to string list c.
All-Star
52171 Points
23259 Posts
Re: Variable in Switch-Case in Razor?
Sep 21, 2018 03:48 PM|mgebhard|LINK
IMHO, still overly complicated. Sort the results in the controller action not the View. As far as I can tell there is no reason for the switch as a simple loop will do fine as long as you format the View Model in the controller first.
All-Star
18815 Points
3831 Posts
Re: Variable in Switch-Case in Razor?
Sep 24, 2018 02:19 AM|Nan Yu|LINK
Hi jj819,
The question you meet may be how to find the list by local variables :
You can use a data structure that's designed for it - in this case, a Dictionary<string, List<string>>. This will allow you to set a name as a key to access an object:
Then you can access a specific list by name through the Dictionary's indexer property:
Then your loop will like something as :
But i would suggest re-design your logic , put the complicated logic on server side .
Best Regards,
Nan Yu