LINQ using NOT IN RRS feed

  • Question

  • User-1967952962 posted

    How to create LINQ query fro the following SQL

    Select * 
      FROM [DEV].[tss].[MERCHANT]
      ParentID IS NULL
      ParentID NOT IN (Select distinct MerchantID FROM [DEV].[tss].[MERCHANT])
       var mm = context.MERCHANTs.OrderBy(e => e.MerchantID).Select(p => new Models.ViewModel.NEWVIEWMODEL.MerchantViewModel
                    MerchantID = p.MerchantID,
                    MerchantName = p.MerchantName,
                    ParentID = p.ParentID,
                    CreatedBy = p.CreatedBy,
                    CreatedDate = p.CreatedDate,
                    ModifiedDate = p.ModifiedDate,
                    ModifiedBy = p.ModifiedBy

    Any help using not in to the above query... appreciated.

    Thank you

    Wednesday, August 29, 2018 2:04 PM


  • User1520731567 posted

    Hi kanmai,

    If you want to implement NOT IN in Linq,I suggest you could use the negation of Contains().

    For example:

    var exceptionList = new List<string> { "exception1", "exception2","exception2" };
    var query = myEntities.MyEntity
                             .Select(e => e.Name)
                             .Where(e => !exceptionList.Distinct().Contains(e));

    More details,you could refer to:


    Best Regards.

    Yuki Tao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 30, 2018 3:26 AM