im trying to remove a few items from a query results using linqCore but its not removing them code& results
IQueryable<int> postOn = //Results=9(Id)
_context.QuettaOffers.Where(d => d.SiteUserId == LoguserId).Select(s => s.QuettaReqId);
//Get all request that relevant to user without the one he already post on
IOrderedEnumerable<QuettaReq> listOfR = //Results= 9,11 (Id) should be ..11(Id)
//IQueryable<QuettaReq> listOfR
_context.Quetta.Include(q => q.Category)
.Where(d => d.OfferDate > DateTime.Now && d.CatId == suplayerCat)
.ToList()
//.ToList()
.SkipWhile(a => a.Id.Equals(postOn))
.OrderBy(x => x.Id);
// .TakeWhile(a => a.Id != postOn);
There are several issues with your code.
First, SkipWhile
/ TakeWhile
are useless for unordered sequences, which are normally the result of EF queries except they include explicit OrderBy
. The standard and more appropriate method for filtering is Where
.
Second, a.Id.Equals(postOn)
resolves to object.Equals
, and since a.Id
is int
and postOn
is IQueryable<int>
, it always evaluates to false
.
What you really need is additional Where
condition based on !Contains
. It could be &&
to the current Where
or just separate Where
(these constructs are treated one and the same way):
_context.Quetta.Include(q => q.Category)
.Where(d => d.OfferDate > DateTime.Now && d.CatId == suplayerCat)
.Where(q => !postOn.Contains(q.Id))
The additional benefit would be that the filtering will happen server side.
SkipWhile
will only skip items in the beginning of the IEnumerable<T>.
Once that condition isn't met it will happily take the rest of the elements. Other elements that later match it down the road won't be skipped.
instead of SkipWhile
you can use Except
var result = QueryResult.Except(a => a.Id.Equals(Id here));