2017-02-14 145 views
0

我已经定义了两个列表,现在要过滤escHistory列表Application属性匹配filterRIDS RID值。如何通过int列表中的属性值过滤列表?

但是,通过LINQ筛选出escHistory,我得到的返回计数为零。虽然列表确实包含应用程序属性值与filterRIDS中定义的应用程序属性值匹配的记录。

问题:

如何通过另一个列表中的属性值过滤列表?

这是过滤器代码的基本要点是:

List<int> filterRIDS = new List<int> { 115841, 200463 }; 
List<Escalation> escHistory = new List<Escalation>(); 

//Filter list by RID's matching 
escHistory = escHistory.Where(r => r.Application.Contains(filterRIDS.ToString())).ToList<Escalation>(); 

回答

3

你换查询

escHistory = escHistory.Where(r => filterRIDS.Contains(int.Parse(r.Application))).ToList(); 
+1

好吧,忘了提及'r.Application'是一个像'123456 MyTestApp'这样的字符串类型。所以在我的情况下,我会得到解析到int之前的子字符串。 'escHistory = escHistory.Where(r => filterRIDS.Contains(int.Parse(r.Application.Substring(0,6))))。ToList(); ' –

+2

@BrianJ或你分裂'r.Application.Split('').First()' –

1

问题是filterRIDS.ToString()返回名称List<int>类:

"System.Collections.Generic.List`1[System.Int32]" 

中当然你没有在中包含这个值的历史对象字段。如果您想检查Application字符串是否包含从给定的列表中的任何字符串:

escHistory = escHistory 
    .Where(r => filterRIDS.Any(id => r.Application.Contains(id.ToString()))) 
    .ToList(); 
0

我认为this是你在找什么。在将int列表转换为字符串时进行小的更改。