如何在剃须刀页面中使用 Array.Contains?
How to use Array.Contains in razorpages?
我目前在使用包含在 cshtml 文件中时遇到问题。
我想检查 devgroup
是否包含 user
.
@{
string[] devgroup = { "55", "53", "75" };
var devdisplay = "none";
var user = ViewBag.IDEmployee;
if (devgroup.Contains(user))
{
devdisplay = "block";
}
}
我目前遇到编译错误。
Error CS1929 'string[]' does not contain a definition for 'Contains' and the best extension method overload 'MemoryExtensions.Contains<T>(ReadOnlySpan<T>, T)' requires a receiver of type 'ReadOnlySpan<T>'
你在这种情况下列出如下
@{
List<string> devgroup = new List<string>(){ "55", "53", "75" };
var devdisplay = "none";
var user = ViewBag.IDEmployee;
if (devgroup.Contains(user))
{
devdisplay = "block";
}
}
或
@{
List<string> devgroup = new List<string>(){ "55", "53", "75" };
var devdisplay = "none";
var user = ViewBag.IDEmployee;
if (if (Array.Exists(devgroup,(i)=> i==user))
{
devdisplay = "block";
}
}
尝试使用:
if (devgroup.ToList().Any(d=>d==user))
或
if (devgroup.ToList().Contains(user))
我目前在使用包含在 cshtml 文件中时遇到问题。
我想检查 devgroup
是否包含 user
.
@{
string[] devgroup = { "55", "53", "75" };
var devdisplay = "none";
var user = ViewBag.IDEmployee;
if (devgroup.Contains(user))
{
devdisplay = "block";
}
}
我目前遇到编译错误。
Error CS1929 'string[]' does not contain a definition for 'Contains' and the best extension method overload 'MemoryExtensions.Contains<T>(ReadOnlySpan<T>, T)' requires a receiver of type 'ReadOnlySpan<T>'
你在这种情况下列出如下
@{
List<string> devgroup = new List<string>(){ "55", "53", "75" };
var devdisplay = "none";
var user = ViewBag.IDEmployee;
if (devgroup.Contains(user))
{
devdisplay = "block";
}
}
或
@{
List<string> devgroup = new List<string>(){ "55", "53", "75" };
var devdisplay = "none";
var user = ViewBag.IDEmployee;
if (if (Array.Exists(devgroup,(i)=> i==user))
{
devdisplay = "block";
}
}
尝试使用:
if (devgroup.ToList().Any(d=>d==user))
或
if (devgroup.ToList().Contains(user))