Get intersection of 2 string arrays

Diego Alvarez 101 Reputation points
2022-04-23T22:12:05.133+00:00

Hello. I try to get the intersection of 2 string arrays using an implementation of IEqualityComparer. But for some reason the Equals method is not executed. I put a breakpoint but the execution doesn't stop there. Do you know the reason? Thank you. I attach the code.

195871-imagen001.png

195805-imagen002.png

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
0 comments No comments
{count} votes

8 answers

Sort by: Most helpful
  1. Andriy Bezkorovayny 6 Reputation points
    2022-04-23T23:44:16.783+00:00

    @Diego Alvarez your code is working fine for me, too. you can find evidence here: https://dotnetfiddle.net/ODIXlc

    1 person found this answer helpful.
    0 comments No comments

  2. LesHay 7,126 Reputation points
    2022-04-23T22:57:29.273+00:00

    Hi
    You need to post code in a code block (5th from left in toolbar), unless you expect people here to copy from your image and retype everything.

    Anywat, can't answer your question as I can't view your code. I think it is about getting matches (or differences) in string arrays - if so, check this image out.

    195744-111.png

    0 comments No comments

  3. Diego Alvarez 101 Reputation points
    2022-04-23T23:31:42.957+00:00

    Hello LesHay-2099. Thank you for your reply. This is the code:

    if (valoresCondicion.Intersect(numeroOperando.Textos, new ComparadorTextosInformacion(
                                                TipoOpcionCondicion_TextosInformacion)).Any())
                                        valorCondicion = true;
    
    
    
    
    public class ComparadorTextosInformacion : IEqualityComparer<string>
        {
            public TipoOpcionImplicacion_AsignacionTextoInformacion TipoOpcionCondicion_TextosInformacion { get; set; }
    
            public ComparadorTextosInformacion(TipoOpcionImplicacion_AsignacionTextoInformacion tipoOpcion)
            {
                TipoOpcionCondicion_TextosInformacion = tipoOpcion;
            }
            public bool Equals(string x, string y)
            {
                switch (TipoOpcionCondicion_TextosInformacion)
                {
                    case TipoOpcionImplicacion_AsignacionTextoInformacion.ContengaTexto:
                        return x.Replace("\t", string.Empty).Trim().ToLower().Contains(y.Replace("\t", string.Empty).Trim().ToLower());
    
                    case TipoOpcionImplicacion_AsignacionTextoInformacion.EmpiecenCon:
                        return x.Replace("\t", string.Empty).Trim().ToLower().StartsWith(y.Replace("\t", string.Empty).Trim().ToLower());
    
                    case TipoOpcionImplicacion_AsignacionTextoInformacion.TerminenCon:
                        return x.Replace("\t", string.Empty).Trim().ToLower().EndsWith(y.Replace("\t", string.Empty).Trim().ToLower());
    
                    case TipoOpcionImplicacion_AsignacionTextoInformacion.TextoDistinto:
                    case TipoOpcionImplicacion_AsignacionTextoInformacion.TextoIgual:
                        return x.Replace("\t", string.Empty).Trim().ToLower().Equals(y.Replace("\t", string.Empty).Trim().ToLower());
                }
    
                return false;
            }
    
            public int GetHashCode(string obj)
            {
                return obj.GetHashCode();
            }
        }
    
    0 comments No comments

  4. Andriy Bezkorovayny 6 Reputation points
    2022-04-23T23:36:50.793+00:00

    Please check the example, the comparer works fine for me, all methods are triggered: https://dotnetfiddle.net/my63GH
    195873-image.png

    0 comments No comments

  5. Diego Alvarez 101 Reputation points
    2022-04-23T23:56:35.847+00:00

    andriyb The code doesn't work for me in my VS project. Will it be a configuration?

    0 comments No comments