I've seen many answers to this - but can't seem to get them to work:
var numinqtoday = bc.SSRecs.Where(x => x.DateTime == id).Select(n => new
{
refer = n.@ref
}).GroupBy(x => x.refer).ToList();
var numinqprev = bc.SSRecs.Where(x => x.DateTime < id).Select(n => new
{
refer = n.@ref
}).GroupBy(x => x.refer).ToList();
var filtered = numinqtoday.Except(numinqprev).ToList();
In my code above:
numinqtoday.Count() = 184
numinqprev.Count() = 155
filtered.Count() = 184
I have checked and there are definitely occurrences within numinqprev within numinqtoday - so it appears the .Except isn't removing them before putting in filtered.
Could anyone advise please where I'm going wrong?
Thank you,
Mark
You've got a list of groups there - and I suspect you'll find that one group is never equal to another. I suspect you want to perform the Except
operation before the grouping. For example:
var numinqtoday = bc.SSRecs.Where(x => x.DateTime == id).Select(n => new
{
refer = n.@ref
}).ToList();
var numinqprev = bc.SSRecs.Where(x => x.DateTime < id).Select(n => new
{
refer = n.@ref
}).ToList();
var filtered = numinqtoday.Except(numinqprev).GroupBy(x => x.refer).ToList();
Another option would be to create a set of keys, and use Where
:
// Code as per your original, including the GroupBy calls. Change the string
// part as per the type of refer, which we don't know...
var keys = new HashSet<string>(numinqprev.Select(g => g.Key));
var filtered = numinqtoday.Where(g => !keys.Contains(g.Key)).ToList();
Also note that if you've only got one property, there's not much point in using an anonymous type.
See more on this question at Stackoverflow