Conversion of a LINQ query into methods

Asked

Viewed 55 times

1

The following query in LINQ (with query) below is used to list all vendors that have an account (relationship 1 to 1):

from f in fornecedores
join c in contas on f.ID equals c.FornecedorID
select f

How can I achieve the same result using LINQ methods with .Select() ... ?

  • 2

    Why do you think it’s not so good?

  • To me it seems OK too, if the consultation was not working would be another conversation. :)

  • The first query is much better than the 2nd. Because wants to trade one for another?

  • Actually wanted to know how it would look in methods (even in practice being the same result). I will edit the question.

1 answer

1


To use Join, it is much better to use syntax instead of methods, since the Join() method has {4.5} parameters, which makes reading more difficult.

Still, answering your question, your query would be:

fornecedores.Join(contas, f => f.ID, c => c.FornecedorID, (f, c) => f);

Parameters:

1º: Collection to which you will join

2nd: Key to the first collection

3rd: Key to the second collection

4º: Function that returns the result item, is almost the same as using Select(), but takes two parameters, the first is the item of the first collection (suppliers), the second is the item of the second collection (accounts).

Browser other questions tagged

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