问题描述
我有以下对象:
Line{ String Content; Type type;}
我有IQeryable<Line>
行,我针对这些行执行操作.我选择了line.Content.Contains('x') = list1
所在的某些行,现在尝试到达其余的行,即lines-list1,为此使用
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);
,但结果为list2 = lines
.
代码:
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);
}
有什么想法吗?
推荐答案
lines
是IQeryable<Line>
.如果不保存其结果,则每次您选择它时都将运行它.如果Line
不覆盖Equals
和==
,则每次都会创建不同的对象,因此Except
不能从新对象中删除先前的对象.
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);
这篇关于IQueryable .Except()不能达到我期望的结果!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!