What’s the opposite of . equals in C#?

Asked

Viewed 200 times

5

I’m doing a left Join where I want to pick up just what’s on the left table and there’s no right one.

I’m using LINQ and Entityframework. I made a code that takes the similarities, follows below:

var mQuery = from pessoa in clientes
                         from prod in produtos
                         on pessoa.grupo equals prod.grupo
                         Select New { pessoa.nome, prod.descricao }

Like I said, I’m training LINQ and Entity. I wanted a method that would necessarily do the opposite of equals. Note: the operator != did not work

Vlww

  • o Linq no em left Join direto, é preciso usar alguns recursos, como agrupar e colocar nulo onde não encontrar, here is an example that can help you: https://docs.microsoft.com/en-us/dotnet/csharp/linq/perform-left-outer-joins

  • The model I picked up is from this link. My problem is not with left Join, which by the way, worked. But with the method of comparing objects

2 answers

2

You can use the except

Except is the LINQ extension method that subtracts elements from a collection. For example, this will remove elements of the first entities (List1) that are elements in the second entities (List2) and that will be returned as a third entity (List3).

var Lista3 = Lista1.Except(Lista2);

Another more complete example:

int[] Lista1 = { 5, 3, 9, 7, 5, 9, 3, 7 };  
int[] Lista2 = { 8, 3, 6, 4, 4, 9, 1, 0 }; 

int[] Lista3 = Lista1.Except(Lista2).ToArray(); 

Console.WriteLine("Resultado:");  
foreach (int num in Lista3){  
  Console.WriteLine("{0} ", num);  
}  

Upshot: 7 and 5.

List1: Full select result.

List2: Select with data that should not appear.

Lista3: Result of the method except, bringing only different data from List2.

Note: That operator is like a not in.

  • I would have to throw Tabela1 and table2 dice on a list?

  • You would need to throw the dice into a array.

  • But then I stop using LINQ and use a common list structure, right?

  • You can continue using LINQ to carry out your select to bring data to List1 and List2.

  • 1

    if you have to load a large select and a large select, it will have a considerable impact on performance...

  • 2

    Agree @Cypherpotato, it would not be very performatic.

  • 1

    @Cypherpotato, I agree. But the challenge here was necessarily to use LINQ. I’m training different ways to do it. So another way of execution, in this case, is not very interesting to me.

Show 2 more comments

1


I found the answer I was looking for. There is no method that is opposite to .Equal. The name of what I was looking for was Outer Left Join. The challenge was that I wanted to use only the LINQ structure.

Goes below:

var permissoesCliente = from permi in context.tabelaPermissoes
                    join aces in context.tabelaAcessos
                    on permi.CodigoPermissao equals 
                    aces.CodigoPermissao into tt
                    from tabelaTemp in pp.DefaultIfEmpty()
                    where tabelaTemp == null
                    select new { permi.CodigoPermissao };

ref: https://forums.asp.net/t/1896048.aspx

Browser other questions tagged

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