-4

Given this classic VB.NET code

For Each item in myColl
   item.PropX = "New Value"
Next

What will be the linq for the same purpose (in VB.NET)?

user229044
  • 232,980
  • 40
  • 330
  • 338
E-Bat
  • 4,792
  • 1
  • 33
  • 58
  • 1
    But you do realize, that this line will *not* actually update any item, right? You will need to materialize it to have any effect. – nvoigt Apr 27 '15 at 14:27
  • Try http://converter.telerik.com/ – jwatts1980 Apr 27 '15 at 14:31
  • @nvoigt, I'm aware of what you said – E-Bat Apr 27 '15 at 14:33
  • 1
    @E-Bat Because you're not updating the object at all, you're only retrieving the property. – Josh L. Apr 27 '15 at 14:34
  • 1
    @E-Bat Look at this link. But it's not usually recommended to use the LINQ ForEach when a normal foreach loop can do the same thing. http://stackoverflow.com/questions/200574/linq-equivalent-of-foreach-for-ienumerablet – Josh L. Apr 27 '15 at 15:03
  • 1
    You should refrain from totally changing a question, particularly when there were **3 answers** at the time of writing. It was a _c# LINQ to VB.NET LINQ_ and now it is _VB.NET `for each` to VB.NET LINQ_ as opposed to the original `var query = myColl.Select(item => { item.PropX = "New Value"; return item; })`. –  Apr 27 '15 at 15:08
  • @E-Bat _"**For c# this is how I do it. ... What will be the linq for VB.NET?**"_ –  Apr 27 '15 at 15:14
  • 1
    @E-Bat It might be a good idea to leave some sort of note in your question that the answers are referring to an _entirely_ different question to what you have now, so that users at first glance will understand that the answers are not wrong but that the question was changed where the answers no longer apply. Something like leaving the original question up there and saying that you are clarifying the question with your new question. – Josh L. Apr 27 '15 at 15:20
  • 2
    @E-Bat Please *don't* edit answers into your question. Your question and the answers below it are not a mutable on-going dialog. If you have a new question about one of the answers you've received, ask another question. If you want to mark your question as solved, then accept the answer that solved your problem. – user229044 Apr 28 '15 at 02:06

1 Answers1

-1

OP's previous question:

For c# this is how I do it:

var query = myColl.Select(item => { item.PropX = "New Value"; return

item; }).ToList()

What will be the linq for VB.NET?

This is probably the structure you're looking for, though I'm not sure this is what you want to do as all this is doing is setting query as an IEnumerable of string, where it's always "New Value".

dim query = myColl.Select(Function(item) item.PropX = "New Value")

UPDATED QUESTION OP did make changes to the question that invalidated most of the answers on this thread. The updated question in case it gets changed again:

For Each item in myColl    item.PropX = "New Value" Next

What will be the linq for the same in VB.NET?

UPDATED ANSWER:

Assuming myColl is of type List:

myColl.ForEach(Function(x) x.PropX = "New Value")

Though this isn't recommended. OP's better off using a normal foreach loop.

Josh L.
  • 454
  • 2
  • 13
  • Agreed. I will edit it as OP updates, but in the event that OP doesn't and people search for the VB.Net translation of the above LINQ expression and it is what they're looking for, I'll keep it up. – Josh L. Apr 27 '15 at 14:33
  • 2
    It feels like you're trying to retrieve the item from the collection (because of return item, which doesn't work that way in LINQ) but I'm not sure. What are you expecting the LINQ code to do? – Josh L. Apr 27 '15 at 14:55
  • myColl.ToList().ForEach(Sub(x As Item) x.PropX = "New Value") That worked – E-Bat Apr 27 '15 at 15:19
  • @JoshL. I have rolled back the OP's question as per _[Should I rollback the OP's edits, if they just replicate my answer's proposed changes?](http://meta.stackoverflow.com/questions/290240/should-i-rollback-the-ops-edits-if-they-just-replicate-my-answers-proposed-ch)_. You should note the part _"and the OP incorporated the proposed solution into their question by editing it. This made the answer a bit of useless"_. While I think your answer is OK, it is a shame perhaps the OP did that –  Apr 27 '15 at 15:29