Preciso fazer um join por LINQ em duas listas usando codigoLista. A lista 1 deve existir na segunda lista (lista 2) e precisa ser retornada. Como faço isso? Obs.: Nunca fiz join via LINQ em c#
1 Resposta
você pode fazer da seguinte forma.:
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
};
Lembrando que não é possível fazer um Right Join
, no máximo você pode fazer um Left Join
invertendo a ordem das coleções.
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
};
Também não é possível fazer o Full Join
de maneira simples, você terá de fazer um Left Join
e um "Right Join"
, então unir ambos.
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);
-
Isso ainda é muito difícil pra mim, mas preciso aprender urgente a construir o LEFT JOIN. 27/07/2016 às 1:44
-
as duas listas são as seguintes: '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");' como ficaria? sendo que FamiliaArea deve estar em Familia 27/07/2016 às 17:04
-
@MarianaDelfino tem como editar a sua pergunta e adicionar este código? 27/07/2016 às 17:38
-
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");
27/07/2016 às 18:41 -
1@MarianaDelfino a sua duvida já escapa ao escopo da pergunta, aconselho que faça uma nova pergunta, não esqueça de adicionar as classes
Familia
eFamiliaArea
(não é possível responder a sua pergunta sem conhecer a estrutura de ambas), detalhe o seu problema, se possível informe o resultado esperado. e por fim, evite colocar código nos comentários, é difícil de ler, entender e copiar, se precisar adicionar alguma informação, edite à pergunta, deixe os comentários paras as duvidas. 28/07/2016 às 0:43