JOIN by LINQ in C#

Asked

Viewed 224 times

0

I need to make a Join by LINQ in two lists using codeList. List 1 must exist in the second list (list 2) and must be returned. How do I do that? Obs.: I never Join via LINQ in c#

1 answer

6


you can do it this way.:

OUTER JOIN

var set1 = new HashSet<Entity1>();
var set2 = new HashSet<Entity2>();

var query =
    from entity1 in set1
    join entity2 in set2 on entity1.Codigo equals entity2.Codigo
    select new {
        Entity1 = entity1,
        Entity2 = entity2
    };

LEFT JOIN

var set1 = new HashSet<Entity1>();
var set2 = new HashSet<Entity2>();

var query =
    from entity1 in set1
    join entity2 in set2 on entity1.Codigo equals entity2.Codigo into lSet
    from entity2 in lSet.DefaultIfEmpty()
    select new {
        Entity1 = entity1,
        Entity2 = entity2
    };

Remembering that it is not possible to make a Right Join, at most you can make a Left Join reversing the order of collections.

RIGHT JOIN

var set1 = new HashSet<Entity1>();
var set2 = new HashSet<Entity2>();

var query =
    from entity2 in set2
    join entity1 in set1 on entity2.Codigo equals entity1.Codigo into rSet
    from entity1 in rSet.DefaultIfEmpty()
    select new {
        Entity1 = entity1,
        Entity2 = entity2
    };

Nor is it possible to do the Full Join in a simple way, you will have to make a Left Join and a "Right Join", then unite both.

FULL JOIN

var set1 = new HashSet<Entity1>();
var set2 = new HashSet<Entity2>();

var lQuery =
    from entity1 in set1
    join entity2 in set2 on entity1.Codigo equals entity2.Codigo into lSet
    from entity2 in lSet.DefaultIfEmpty()
    select new {
        Entity1 = entity1,
        Entity2 = entity2
    };

var rQuery =
    from entity2 in set2
    join entity1 in set1 on entity2.Codigo equals entity1.Codigo into rSet
    from entity1 in rSet.DefaultIfEmpty()
    select new {
        Entity1 = entity1,
        Entity2 = entity2
    };

var query = lQuery.Union(rQuery);
  • This is still very difficult for me, but I need to learn urgently to build LEFT JOIN.

  • as duas listas são as seguintes: 'List<Familia> familiasFxa = Bo.ExecuteProcedureList<Familia>(connectionString, "STP_SEL_Familia");&#xA; familiasFxa.Insert(0, new Familia() { CodFamilia = "0", DescricaoFamilia = "Selecione a Familia" });&#xA; return familiasFxa; List<Familiaarea> familiasAreaFxa = Bo.Executeprocedurelist<Familiaarea>(connectionString, "Stp_sel_familiaarea");' how would you look? Familiaarea must be in Familia

  • @Marianadelfino has how to edit your question and add this code?

  • List<Familia> familiasFxa = Bo.ExecuteProcedureList<Familia>(connectionString, "STP_SEL_Familia"); familiasFxa.Insert(0, new Familia() { CodFamilia = "0", DescricaoFamilia = "Selecione a Familia" }); return familiasFxa; List<FamiliaArea> familiasAreaFxa = Bo.ExecuteProcedureList<FamiliaArea>(connectionString, "STP_SEL_FamiliaArea");

  • @Tobymosque This Join is very important think. It happens the following: There is a general group of families of products. The products will have their prices changed. At first, the families should be selected. Let’s assume, I selected only the family "Stationery" and "Food". In the second moment, when it is applied the percentage of discount should appear only the options of "Stationery" and "Food" and in my case appear all families, even those that have not been defined. I believe that a Join by Linq can solve the problem but I do not have ctz tb.

  • 1

    @Marianadelfino your doubt already escapes the scope of the question, I advise you to ask a new question, do not forget to add the classes Familia and FamiliaArea (cannot answer your question without knowing the structure of both), detail your problem, if possible inform the expected result. and finally, avoid putting code in comments, it is difficult to read, understand and copy, if you need to add some information, edit to the question, leave comments to doubts.

  • @Tobymosque I came to a solution taking doubts with the development team. anyway your code helped a lot, thank you!

Show 2 more comments

Browser other questions tagged

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