Filename coming from a txt and files. Is there a difference in comparison?

Asked

Viewed 49 times

1

I created a list with 13 filenames, which I extracted using Getfilename(). Another list I broke a txt file and loaded these names into another list. So I have this:

ListaNomeArquivo

ListaNomeTxt

I need to get the name of the Listfilename list and see if it exists within Listfilename. Both Contains and Equals, always returns different and I have the same names. Just to illustrate. I have that name on both lists:

web\\ace\\asp\\ace0003a.asp

Turns out the lambda I did says they’re different:

 var busca = listaCommiter.Where(l => !l.Contains(listaFarm.ToString()));

How do I only store what doesn’t really exist?

The complete code of this routine:

private List<string> ComparaArquivo()
        {
            List<string> listaCommiter = new List<string>();
            List<string> listaFarm = new List<string>();
            List<string> listaDiferenca = new List<string>();

            try
            {
                listaCommiter = _listaCommiter();
                listaFarm = _listaFarm();

                var busca = listaCommiter.Where(l => !l.Contains(listaFarm.ToString()));                

                return listaDiferenca;
            }
            catch(Exception ex)
            {
                string r = ex.Message;
                return null;
            }

        }

The list in return listaDiferenca; is returning nothing because I’m still developing. It will return busca.

  • But you want to return only what you have in listaFarm and does not have in listaCommiter or anything that is not in both?

2 answers

1

Precisely for this case, use the Except:

var busca = listaCommiter.Except(listaFarm);

He will return in quest, all of listaCommiter that does not exist in the listaFarm.

1

To return only what you have in listaFarm but is not in listaCommiter, just reformulate your query:

var diferenca = listaCommiter.Where(l => !listaFarm.Contains(l));

You can also use the Enumerable.Except<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)

var diferenca = listaCommiter.Except(listaFarm);

If you still only want what is common between the two, you can use the Enumerable.Intersect<TSource>(IEnumerable<TSource>, IEnumerable<TSource>)

var comum = listaCommiter.Intersect(listaFarm);

I hope I helped the/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.