Home > database >  Convert query SQL Server in ASP.NET MVC
Convert query SQL Server in ASP.NET MVC

Time:06-28

I want to use the SQL Server query below in .NET MVC

select *
from table1
where a = 1
and b not in (select fk_b from table2 where c = 1)

But I don't know how?

entity.table1.Where(record => record.a== 1 && record ....???

CodePudding user response:

In LINQ query syntax, the query should be:

var result = (
    from a in entity.table1
    where a.a == 1
    and  
    !(
        from b in entity.table2
        where b.c == 1
        select b.fk_b
    ).Contains(a.b)
    select a
).ToList();

While in method expression,

var result = entity.table1.Where(record => record.a == 1
    && !entity.table2.Select(y => y.fk_b).AsEnumerable().Contains(record.b))
    .ToList();
  • Related