1

I want to select all the records where FeeZoneID doesn't exist in second linq output.

I know it can easily done in SQL using NOT IN(....) but i do it in LINQ???

from d in FeeZones
where d.FeeZoneID !(from b in FeeZoneSchemes
    where b.FeeSchemeID == 1
    join c in FeeZones on b.FeeZoneID equals c.FeeZoneID
    select c)
 select d

In my C# class this is how I am doing :

   public IList<FeeZone> _LQ_GetAllAvaliableFeeZoneForGivenFeeSchemeID(int FeeSchemeID)
    {
       using(var db = new QualificationContext())
       {
           var query = (from d in db.FeeZone
                        where (!(from b in db.FeeZoneScheme
                               where b.FeeSchemeID == FeeSchemeID
                               join c in db.FeeZone on b.FeeZoneID equals c.FeeZoneID
                               select c).Contains(d.FeeZoneID))
                       select d).ToList();



           return query.ToList();
       }

    }

And this is the error : enter image description here Many Thanks

4 Answers 4

4

Try this code:

var query = from d in FreeZones
            let unwantedZonesIds = FeeZoneSchemes.Where(b => b.FreeSchemeID == 1)
                                                 .Select(b => b.FreeZoneID)
            where !unwantedZonesIds.Contains(d.FreeZoneID)
            select d;
2

Use Any instead of Contains

var query = (from d in db.FeeZone
                    where (!((from b in db.FeeZoneScheme
                           where b.FeeSchemeID == FeeSchemeID
                           join c in db.FeeZone on b.FeeZoneID equals c.FeeZoneID
                           select c).Any(x => x.FeeZoneID ==d.FeeZoneID))
                   select d).ToList();
1

Please try is as below.

from d in FeeZones
where (!(from b in FeeZoneSchemes where b.FeeSchemeID == 1
    join c in FeeZones on b.FeeZoneID equals c.FeeZoneID
    select c.FeeZoneID).Contains(d.FeeZoneID))
 select d
2
  • no i am getting error i have update my code above please... many thanks
    – K.Z
    Feb 10, 2014 at 13:01
  • it return me all the records instead of filtered records i just debug in visual studio...
    – K.Z
    Feb 10, 2014 at 13:16
0

You can use the following query .

        IList<FeeZones> lst = new List<FeeZones>() { new FeeZones() { FeeZoneID = 1 }, new FeeZones() { FeeZoneID =2 } };
        IList<FeeZoneSchemes> lstnew = new List<FeeZoneSchemes>(){new FeeZoneSchemes(){FeeSchemeID=1,FeeZoneID=1}};


        var res = (from d in lst
                   where !(
                       from c in lstnew 
                       join f in lst on c.FeeZoneID equals f.FeeZoneID
                       where c.FeeSchemeID == 1 && c.FeeZoneID == f.FeeZoneID 
                       select f.FeeZoneID
                   ).Contains(d.FeeZoneID)
                   select d).ToList();

or You can use the Sql way of exists query like below.

 var res = (from d in lst
                   where !(
                       from c in lstnew 
                       where c.FeeSchemeID == 1 && c.FeeZoneID == d.FeeZoneID 
                       select c
                   ).Any()
                   select d).ToList();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.