tags:

views:

62

answers:

2

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?

A: 

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);
Kobi
i will mark this as the answer since it has little bit of an explanation that everyone else could use.
OneDeveloper
Thanks. I deleted it yesterday, somehow your edit made it look wrong. Sorry for that.
Kobi
+2  A: 

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

OneDeveloper