我正在使用Linq从文本文件中选择和处理行。我的txtfile是由管道符“|”分隔的两列。该文件包含以下内容:
HAbbe | 11
GABBOT | 22
DABDA | 33
RAchant | 44
RADA | 55
DABDA | 66
您会注意到第3行和第6行有一个重复的ID(第1列)。我想使用linq最初读取发布的txt文件找到重复(并报告它)然后我想从ling查询中选择只有不重复的行。以下是我所拥有的:
StreamReader srReader = new StreamReader(fUpload.PostedFile.InputStream);
var query1 =
from line in srReader.Lines()
let items = line.Split('|')
select new UploadVars()
{
ID = items[0],
Number = items[1]
};
var GroupedQuery = from line in query1
group line by line.ID into grouped
where grouped.Count() > 1
select new {
ID = grouped.Key,
MCount = grouped.Count()
};
StringBuilder sb = new StringBuilder();
foreach (var item in GroupedQuery)
{
sb.AppendFormat("The following external ID's occur more than once and have not been processed:<br> {0}. Duplicated {1} times.", item.ID, item.MCount);
}
这一切都很好,并给我正确的结果。我现在希望选择除文本文件中的2个重复行之外的所有行。我编写了以下linq语句但由于某种原因我没有运气:
//lets start at the beginnnig of the the posted filestream
fUpload.PostedFile.InputStream.Position = 0;
srReader = new StreamReader(fUpload.PostedFile.InputStream);
var query2 = from line in srReader.Lines()
let items = line.Split('|')
select new UploadVars()
{
ID = items[0],
Number = items[1]
};
var qryNoDupedMems = from Memb in query2
where !(from duped in GroupedQuery
select duped.ID)
.Contains(Memb.ID)
select Memb;
qryNoDupedMems的结果是文本文件的完整列表。有人可以解释我在这里做错了什么...... 在此先感谢
答案 0 :(得分:2)
在group
查询中,grouped
变量也是IEnumerable
,其中包含组中的tems。
因此,您可以写下以下内容:
var nonDuplicates = from line in query1
group line by line.ID into grouped
where grouped.Count() == 1
select grouped.First()