我正在尝试从单词列表中找到所有等于名称的标签。
例如:-
public class Tag
{
public int Id { get; set; }
public string Name { get; set; }
public string UserId { get; set; }
}
// Arrange.
var searchWords = new List<string>(new [] {"c#", ".net", "rails"});
var tags = new Tags
{
new Tag { Name = "c#" },
new Tag { Name = "pewpew" },
new Tag { Name = "linq" },
new Tag { Name = "iis" }
};
// Act.
// Grab all the tags given the following search words => 'c#' '.net' and 'rails'
// Expected: 1 result.
var results = ???
// Assert.
Assert.NotNull(results);
Assert.Equal(1, results.Count);
Assert.Equal("c#", results.First());
我一直在尝试使用 Any 或 Contains,但我的代码无法编译。
注意:可以是.NET 4.0
请您参考如下方法:
这对你有用吗?
var results = tags.Where(t =>
searchWords.Contains(t.Name, StringComparer.InvariantCultureIgnoreCase));
另请注意,由于 results是IEnumerable<T>您将需要使用方法 results.Count()而不是属性 results.Count在你的断言中。 Count是由 ICollection 定义的属性界面。
