试图通过.NET Linq中where子句的集合找到一些标签

Pur*_*ome 1 .net c# linq .net-4.0

我正在尝试从单词列表中找到所有与名称相等的标签.

例如 :-

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());
Run Code Online (Sandbox Code Playgroud)

我一直在尝试使用AnyContains但我的代码只是不编译.

注意:可以是.NET 4.0

Kev*_*mey 6

这对你有用吗?

var results = tags.Where(t => 
    searchWords.Contains(t.Name, StringComparer.InvariantCultureIgnoreCase));
Run Code Online (Sandbox Code Playgroud)

还要注意,由于resultsIEnumerable<T>你需要使用的方法results.Count(),而不是财产results.Count在你的断言. Count是由ICollection接口定义的属性.