Search code examples
entity-frameworklinqlinq-to-entities

Syntax of NOT IN usage of LINQ


I have a System.Data.Entity.Dbset provided to me by the DataContext of EntityFramework. I have an ObservableCollection AllTaxes in my ViewModel i simply want to delete all those entries from System.Data.Entity.Dbset which have been deleted from AllTaxes,based on a common property called "TaxCode" using Linq.

I m not very good with Linq.I used the following Linq to find out all TaxCodes deleted from ViewModel but still existing in Model.

var List = Hexa_entity.hexa_tax.Where(Left => !AllTaxes.Any(Right => Right.TaxCode == Left.TaxCode));

However,this is giving me the exception:-

Unable to create a constant value of type Hexa.Models.DM_Tax. Only primitive types or enumeration types are supported in this context.

Simply put ,How do i convert the SQL query in between to linq?

List<ClassTest> Lst = 
    select * from List1<ClassTest>() 
    where id not in (select id from List2<ClassTest>())
    .ToList<ClassTest>();

Solution

  • To fix that issue try the following:

    var AllTaxesCodes=AllTaxes.Select(e=>e.TaxCode);//Use a collection of primitive type
    var List = Hexa_entity.hexa_tax.Where(Left => !AllTaxesCodes.Contains( Left.TaxCode));