Linq查询List <int>中的任何项目属于参数string []

时间:2016-04-20 08:42:20

标签: c# linq

代码:

string[] seperators = { "," };
string[] typesList = types.Split(seperators, StringSplitOptions.RemoveEmptyEntries);
string[] topicsList = topics.Split(seperators, StringSplitOptions.RemoveEmptyEntries);

if (typesList.Length > 0)
    filter = filter & builder.Where(t => typesList.Contains(t.Type));
if (topicsList.Length > 0)
    filter = filter & builder.Where(t => topicsList.Any(id => t.Topics.Any(p => id == p.ToString())));
   // above doesn't work

我的班级有以下属性:

public List<int> Topics { get; set; }
public string Type { get; set; }

如何编写查询以使其工作?

编辑:

我可能不清楚你在评论中提到的所以让我们专注于这个代码:

var list = new List<MyClass>();
list = LoadElements();
string[] parametr = { "8", "11" };

var result = list.Where(pm => pm.Topics.Any(t=> t.Equals(8) == true )).ToList();
//this above works for static '8' but I want to query through my 'parametr' list. 

MyClass对象是:

public class MyClass
{
    public string    Data { get; set; }
    public List<int> Topics{ get; set; }
}

1 个答案:

答案 0 :(得分:0)

var result = list.Where(record => parametr.Any(parm => record.Topics.Any(t => parm == t.ToString()))).ToList();