本文介绍了IQueryable .Except()不能达到我期望的结果!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下对象:

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); 
    }

有什么想法吗?

推荐答案

linesIQeryable<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()不能达到我期望的结果!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:22