No, since LINQ is not meant to produce side effects
But you can do that with a simple for loop
Public Class A
Public Property Value As String
End Class
Public Class B
Public Sub Foo()
Dim AList As New List(Of A)
AList.Add(New A)
AList.Add(New A)
AList.Add(New A)
Dim valueList As New List(Of String)
valueList.Add("A")
valueList.Add("B")
valueList.Add("C")
For i As Integer = 0 To AList.Count - 1
AList(i).Value = valueList(i)
Next
AList.ForEach(Sub(a) Console.WriteLine(a.Value))
End Sub
End Class
Outputs
A
B
C
Note:
The .ForEach
extension is a member of List<T>
, and not IEnumerable<T>
, which is what is available in LINQ. For a complete list of what you can do with LINQ, see https://msdn.microsoft.com/en-us/library/system.linq.enumerable(v=vs.110).aspx
Technically, you can use LINQ expressions to accomplish a similar thing. But it is just a way to get around the guideline that LINQ shouldn't produce side effects
Dim AList As New List(Of A)
AList.Add(New A)
AList.Add(New A)
AList.Add(New A)
Dim valueList As New List(Of String)
valueList.Add("A")
valueList.Add("B")
valueList.Add("C")
Alist = AList.Select(
Function(a, i)
' the assignment is now inside the selector, which is not what
' the selector is meant to do which is transform the elements
a.Value = valueList(i)
Return i
End Function).ToList()
AList.ForEach(Sub(a) Console.WriteLine(a.Value))
This probably wouldn't feel right to most developers