OneDeveloper
OneDeveloper

Reputation: 516

IQueryable .Except() is not resulting what I expect!

I have the following object:

Line{ String Content; Type type;}

And I have, IQeryable<Line> lines, which I perform operations against. I selected certain lines where line.Content.Contains('x') = list1, and now am trying to get to the rest of the lines i.e. lines - list1 and for this am using

list2 = lines.Except(list1); 

but that results in list2 = lines.

Code:

private
     IQueryable<Line>
     ExtractLines(
     IQueryable<Line> allLines, 
     String keyword, 
     ref IQueryable<Line> hits)
    {

        hits = allLines.Where(lx => lx.Content.Contains(keyword));  

        return allLines.Except(hits); 
    }

any ideas?

Upvotes: 2

Views: 3126

Answers (2)

OneDeveloper
OneDeveloper

Reputation: 516

Alright. All I needed to do is to implement IEqualityComparer<T> in Line class.

Upvotes: 2

Kobi
Kobi

Reputation: 137997

lines is IQeryable<Line>. If you do not save its result, it will run every time you select from it. If Line does not override Equals and ==, that will create different objects each time, so Except cannot remove the previous object from new objects.

Now, a lot is missing, but try:

var linesList = lines.ToList(); // get results ones
var hasX = lines.Where(line => line.Content.Contains('x'));
var noX = lines.Except(hasX);

Upvotes: 1

Related Questions