0

我有 2 个不同类型的集合。我想匹配这些集合中的字符串并返回不匹配的集合。

1) ac_CategoryList
2) mw_CharityList

如果ac_CategoryList .Title 在mw_CharityList.EntryTitle中,想匹配。如果不存在,则返回不匹配的ac_CategoryList集合项。并返回一个与 ac_CategoryList.Title 匹配的 mw_CharityList 类型的集合。因为我需要更新mw_CharityList集合中的状态。

var var charityList = _db.mw_CompetitionsEntry.Where(e => e.IsInvalid == false && e.IsPublished).ToList(); // first get the entire valid collection
var categoryList = _db.ac_Category.Where(c => c.Title != null && c.IsDeleted == false).ToList(); // get the entire valid collection

var titleNotExitsCollection = categoryList.Where(c => charityList.Any(e => e.EntryTitle.Trim() != c.Title.Trim())).ToList();
var titleExitsCollection = charityList.Where(e => categoryList.Any(c => c.Title.Trim() == e.EntryTitle.Trim())).ToList();

现在 titleNotExitsCollection 和 titleExitsCollection 返回相同的记录数。我不知道我做错了什么......请帮忙

4

2 回答 2

2

看起来缺少一个 not 运算符,请尝试:

var titleNotExitsCollection = categoryList.Where(c => !charityList.Any(e => e.EntryTitle.Trim() == c.Title.Trim())).ToList();
于 2013-10-09T09:44:22.987 回答
1
var commonTitles = categoryList.Select(x=>x.Title.Trim())
                               .Intersect(charityList.Select(x=>x.EntryTitle.Trim()));
var titleNotExitsCollection  = categoryList.Where(x=>!commonTitles.Contains(x.Title))
                                           .ToList();
var titleExitsCollection = charityList.Where(x=>commonTitles.Contains(x.EntryTitle))
                                      .ToList();
于 2013-10-09T09:44:23.840 回答