Home > Blockchain >  How to use Array.Contains in razorpages?
How to use Array.Contains in razorpages?

Time:03-03

I'm currently having a problem using Contains inside cshtml file. I want to check if devgroup contains a user.

@{ 
                                    string[] devgroup = { "55", "53", "75" };
                                    var devdisplay = "none";
                                    var user = ViewBag.IDEmployee;
                                    if (devgroup.Contains(user))
                                    {
                                        devdisplay = "block";
                                    }

                                }

and I'm currently having an compile error.


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>'

CodePudding user response:

You List in this case like below

@{
        List<string> devgroup = new List<string>(){ "55", "53", "75" };
        var devdisplay = "none";
        var user = ViewBag.IDEmployee;
        if (devgroup.Contains(user))
        {
            devdisplay = "block";
        }

    }

or

@{
        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";
        }

    }

CodePudding user response:

Try to use:

if (devgroup.ToList().Any(d=>d==user))

or

if (devgroup.ToList().Contains(user))
  • Related