MatchCollection Contains

ANB 181 Reputation points
2021-11-23T21:06:37.257+00:00

I have the following regex that will return me 4 items:
MatchCollection matchesFiles = Regex.Matches(content, regexFiles, RegexOptions.IgnoreCase | RegexOptions.Singleline);

Result:
[0] { file="banner.jpg" width="100" }
[1] { file="footer.jpg" width="100" }
[2] { file="leftSide.jpg" width="50" }
[3] { file="footer.jpg" width="100" }

Now I want to add to a List, all of them except duplications:
List<FileModel> fileArray = new List<FileModel>();

foreach (Match m in matchesFiles )
if (!matchesFiles.Contains(m.file) {
fileArray.Add( ... )
}

But somehow contains doesnt work...
Why ?
Thx

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,184 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Lan Huang-MSFT 23,306 Reputation points Microsoft Vendor
    2021-11-24T07:12:49.687+00:00

    Hi @ANB ,
    The MatchCollection.ICollection<Match>.Contains(Match) method determines whether the collection contains a specific value.

    bool ICollection<Match>.Contains (System.Text.RegularExpressions.Match item);

    You can check https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.matchcollection.system-collections-generic-icollection-system-text-regularexpressions-match--contains?view=net-6.0
    Maybe you can change the method, you can refer to this demo:
    controller:

    public ActionResult Index()  
    {  
         List<FileModel> matchesFiles = new List<FileModel>()  
         {  
                     new FileModel { file="banner.jpg", width="100" },  
                     new FileModel { file="footer.jpg", width="100" },  
                     new FileModel { file="leftSide.jpg", width="50" },  
                     new FileModel { file="footer.jpg", width="100" },  
                };  
                List<FileModel> fileArray = new List<FileModel>();  
                foreach (FileModel item in matchesFiles)  
                {  
                    if (!fileArray.Any(f => f.file == item.file))  
                    {  
                            fileArray.Add(item);  
                    }  
                }  
                ViewBag.fileArray = fileArray;  
                return View();  
            }  
    

    model:

    public class FileModel  
        {  
            public string file { get;set;}  
            public string width { get; set; }  
      
        }  
    

    .cshtml:

    <html>  
    <head>  
        <meta name="viewport" content="width=device-width" />  
        <title>Index</title>  
    </head>  
    <body>  
      
        <h3>fileArray</h3>  
        <table border="1" cellpadding="2" cellspacing="2">  
            <tr>  
                <th>Id</th>  
                <th>Name</th>  
            </tr>  
            @foreach (var fileArray in ViewBag.fileArray)  
            {  
                <tr>  
                    <td>@fileArray.file</td>  
                    <td>@fileArray.width</td>  
                     
                </tr>  
            }  
        </table>  
      
    </body>  
    </html>  
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments