How can I filter the records of one table that are not in another with linq C #?

0

For example I have a table "table1" with the fields id and name. I have another table "table2" with the fields id, idtabla1 and description. What I'm looking for is to make a query with linq that returns the records in table1 that do not appear in table2.

    
asked by DenBleyker 17.04.2017 в 22:13
source

2 answers

1

What you're looking for is a left join in linq

How to: Perform left outer join operations

it could be something like

var query = from t1 in tabla1
            join t2 in tabla2 on t1.id  equals ts.idtabla1  into g
            from subt2 in g.DefaultIfEmpty()
            select new { 
                t1.nombre, 
                t2 = (subt2 == null ? String.Empty : subt2.descripción) 
            };
    
answered by 17.04.2017 в 22:26
1

It seems to me that what you are looking for is what is achieved in SQL with a condition NOT IN , which in LINQ would be something like this:

var query =
    from t1 in tabla1
    where !(
        from t2 in tabla2
        select t2.idtabla1
    ).Contains(t1.id)
    select t1;
    
answered by 17.04.2017 в 22:54