Use of except in a lambda expression

Asked

Viewed 163 times

0

I have that expression and it doesn’t work:

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

I’ve already removed Tostring() and still nothing. listaCommiter and listaFarm are two string lists. I tried with Contains and even the files existing on both lists he says are different. A list I fill with Getfilename and the other I Split a txt file and assemble the list. I think the txt list, must have strange characters and so the difference, I removed the \n and also the \r, but there may be something else. Well, as I compare the two lists with lambda?

  • What is your intention? Create a new list with all the data of the second that does not exist in the first? (This was an assumption)

  • @jbueno, exactly that. I need to create a list and then delete from the file the names that are in the Commiter list and are not in the Farm list.

  • I did so and it worked: var busca = listaCommiter.Except(listaFarm);. I just couldn’t do it with lambda.

1 answer

1


I can’t understand why you used the Where, you do not need it. It is only necessary to use the Except to have the expected result.

Documentation of the method Except says:

Produces the difference of two sequences using the standard equalizer to compare values.

Take an example:

var lista = new [] {"nome", "teste", "texto", "outro"};    
var lista2 = new [] {"nome", "teste", "texto", "outro2"};

var excecoes = lista2.Except(lista).ToList();

excecoes.ForEach(WriteLine);

The exit will be outro2.

See on dotNetFiddle

Browser other questions tagged

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