我在使用此Linq查询时遇到问题。我有一个数据表(包括一个名称列),其中包含我要处理的所有数据。我想对此数据表执行Linq查询,但我想排除myNegativeList中的名称,其中> = 15。
myNegativeList具有(Name =" John&#34 ;; Amount = 5)不应从Linq查询中排除John 。 myNegativeList也有(Name =" Sally&#34 ;; Amount = 100)Sally 应该从Linq查询中排除。
Class ListItems
Public Name As String
Public Amount As Decimal
End Class
Sub GetList()
'get data table
Dim NoticeTable As DataTable = GetTable 'has Name and other data
'get my list of names I don't want
Dim myNegativeList As List(Of ListItems) = getMyList
'Psuedo code here
Dim Cust = From Notice In NoticeTable _
Where Notice.Name not in (Select Name from myList where Amount >= 15)
End Sub
如何进行除名称之外的Linq查询(存在于myNegativeList和amount> = 15中)
答案 0 :(得分:1)
试试这个: -
Dim result = From dr In NoticeTable.Rows
Where Not myNegativeList.Any(Function(x) x.Amount >= 15 AndAlso x.Name = dr("Name"))
Select dr("Name")
我不确定为什么.Net Fiddle不支持DataTable,但您可以复制我尝试过的Here代码。
答案 1 :(得分:1)
尝试这样做:
Dim Cust = From row In NoticeTable.AsEnumerable() _
Let name = row.Field(Of String)("Name") _
Where Not myNegativeList.Where(Function(c) c.Amount >= 15 ) _
.Any(Function(c) c.Name = name) _
Select row
查看here以获取有关如何使用DataTable进行Linq的另一个示例