2011-04-13 97 views
1

我使用LINQ - VB.Netvb.net - LINQ - 过滤列表对象

我想通过传递String对象在哪里我的过滤列表对象。

Dim _permittedTransactionCodes As List(Of String) = Nothing 'String object 

它填充了数据。

Dim tap100transCodeInfos As List(Of Tap100transCodeInfo) = _dal.GetActiveTap100transCodeByHdrScreenCde(UCase(screenId), "TRAN_CDE") 

我试图下面的东西,但没有得到过滤后的结果

tap100transCodeInfos.Where(Function(x) _permittedTransactionCodes.Contains(x.TranCde)) 

什么想法?

感谢

回答

4

确保将Where函数的输出分配给一个变量。 Where不会过滤现有列表,但会返回一个新的过滤列表。

Dim result As IEnumerable(Of Tap100transCodeInfo) = _ 
    tap100transCodeInfos.Where(Function(x) _ 
     _permittedTransactionCodes.Contains(x.TranCde) _ 
    ) 

然后result应该有过滤结果。

编辑:

Where返回IEnumerable(Of T)因此,如果您分配输出到你需要追加.ToList()到语句的结束(或定义tap100transCodeInfos是从一开始就IEnumerable(Of Tap100transCodeInfo))相同的变量。

tap100transCodeInfos = _ 
    tap100transCodeInfos.Where(Function(x) _ 
     _permittedTransactionCodes.Contains(x.TranCde) _ 
    ).ToList() 
+0

“无法投型的 'WhereListIterator'1 [RTS.BusinessEntities.Tap100transCodeInfo]' 为类型 'System.Collections.Generic.IList'1 [RTS.BusinessEntities.Tap100transCodeInfo]' 对象”。 – user706311 2011-04-13 15:52:48

+0

@ user706311增加了一个附录。 – diceguyd30 2011-04-13 16:47:32