question

DiegoAlvarez-3139 avatar image
0 Votes"
DiegoAlvarez-3139 asked DiegoAlvarez-3139 answered

Get intersection of 2 string arrays

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


dotnet-csharpdotnet-visual-basic
imagen001.png (5.7 KiB)
imagen002.png (43.6 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LesHay-2099 avatar image
0 Votes"
LesHay-2099 answered

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



111.png (125.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DiegoAlvarez-3139 avatar image
0 Votes"
DiegoAlvarez-3139 answered

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();
         }
     }


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

andriyb avatar image
0 Votes"
andriyb answered

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



image.png (68.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

andriyb avatar image
1 Vote"
andriyb answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DiegoAlvarez-3139 avatar image
0 Votes"
DiegoAlvarez-3139 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

andriyb avatar image
0 Votes"
andriyb answered andriyb edited

@DiegoAlvarez-3139 I provided you with two snippets, see the urls to DotNetFiddle, the second one contains the comparer created by you and I see it works properly.
If it doesn't work for you please share more code, explain the way of how did you check the solution and I hope I will help you to get the code working on your side.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DiegoAlvarez-3139 avatar image
0 Votes"
DiegoAlvarez-3139 answered JackJJun-MSFT commented

andriyb, there is no more related code.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@DiegoAlvarez-3139, based on my test, the code also works for me, then I want to know some project information and environment information. What is your current project, is console app(.net) or console app(.net framework app)? Which version of visual studio you are using?

0 Votes 0 ·
DiegoAlvarez-3139 avatar image
0 Votes"
DiegoAlvarez-3139 answered

Thank you but I did the method manually and it works for me.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.