Update List Using Linq In Vb.net

Example 6 – Searching an Existing List (Of Type) Using LINQ to Objects. In this example, a typed list is created (as in the previous example) using LINQ to SQL, populated, and then the returned typed list is queried using LINQ to Objects. LINQ to SQL does not give you a way to specify a set-based update against the database. The only way to update multiple entities/rows is to retrieve, modify and update them via individual SQL UPDATE statements when you call SubmitChanges. You can, however, use SQL directly to do this.

Active4 months ago

LINQ to SQL does not give you a way to specify a set-based update against the database. The only way to update multiple entities/rows is to retrieve, modify and update them via individual SQL UPDATE statements when you call SubmitChanges. You can, however, use SQL directly to do this. Using linq to update items in a list collection hi, i have a LIST and a dataset. I'm using LINQ to join the 2 data objects together and my final result is i want to update the items in the list with linq joined records.

Is there a way to do the following using LINQ?

To clarify, I want to iterate through each object in a collection and then update a property on each object.

My use case is I have a bunch of comments on a blog post, and I want to iterate through each comment on a blog post and set the datetime on the blog post to be +10 hours. I could do it in SQL, but I want to keep it in the business layer.

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
lomaxxlomaxx
58.9k55 gold badges134 silver badges172 bronze badges

16 Answers

While you can use a ForEach extension method, if you want to use just the framework you can do

The ToList is needed in order to evaluate the select immediately due to lazy evaluation.

Amirhossein Mehrvarzi
7,4434 gold badges30 silver badges54 bronze badges
Cameron MacFarlandCameron MacFarland
55k18 gold badges95 silver badges125 bronze badges
Adi Lester
20.4k12 gold badges78 silver badges97 bronze badges
Ε Г И І И ОΕ Г И І И О
5,7471 gold badge25 silver badges39 bronze badges
Snake Eyes
8,68226 gold badges86 silver badges164 bronze badges
RahulRahul

I actually found an extension method that will do what I want nicely

lomaxxlomaxx
58.9k55 gold badges134 silver badges172 bronze badges

Use:

I am not sure if this is overusing LINQ or not, but it has worked for me when wanting to update a specific items in the list for a specific condition.

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
HennishHennish

There is no built-in extension method to do this. Although defining one is fairly straight forward. At the bottom of the post is a method I defined called Iterate. It can be used like so

Iterate Source

JaredParJaredPar
601k127 gold badges1107 silver badges1368 bronze badges

I've tried a few variations on this, and I keep going back to this guy's solution.

Again, this is somebody else's solution. But I've compiled the code into a small library, and use it fairly regularly.

I'm going to paste his code here, for the off chance that his site(blog) ceases to exist at some point in the future. (There's nothing worse than seeing a post that says 'Here is the exact answer you need', Click, and Dead URL.)

granadaCoder

Vb Linq Examples

granadaCoder
16.1k5 gold badges56 silver badges85 bronze badges

No, LINQ doesn't support a manner of mass updating. The only shorter way would be to use a ForEach extension method - Why there is no ForEach extension method on IEnumerable?

Community
Aaron PowellAaron Powell
20.8k13 gold badges88 silver badges146 bronze badges
AnthonyWJonesAnthonyWJones
167k30 gold badges221 silver badges292 bronze badges

You can use LINQ to convert your collection to an array and then invoke Array.ForEach():

Obviously this will not work with collections of structs or inbuilt types like integers or strings.

Tamas CzinegeTamas Czinege
86.7k36 gold badges140 silver badges168 bronze badges

I wrote some extension methods to help me out with that.

I am using it like this:

For reference the argument check:

PartTimeIndiePartTimeIndie

Although you specifically asked for a linq-solution and this question is quite old I post a non-linq-solution. This is because linq (=lanuguage integrated query) is ment to be used for queries on collections. All linq-methods don´t modify the underlying collection, they just return a new one (or more precise an iterator to a new collection). Thus whatever you do e.g. with a Select doesn´t effect the underlying collection, you simply get a new one.

Of course you could do it with a ForEach (which isn´t linq, by the way, but an extension on List<T>). But this literally uses foreach anyway but with a lambda-expression. Apart from this every linq-method internally iterates your collection e.g. by using foreach or for, however it simply hides it from the client. I don´t consider this any more readable nor maintainable (think of edit your code while debugging a method containing lambda-expressions).

Having said this shoulnd´t use Linq to modify items in your collection. A better way is the solution you already provided in your question. With a classic loop you can easily iterate your collection and update its items. In fact all those solutuions relying on List.ForEach are nothing different but far harder to read from my perspective.

So you shouldn´t use linq in those cases where you want to update the elements of your collection.

HimBromBeereHimBromBeere
25.5k4 gold badges34 silver badges65 bronze badges
Bill ForneyBill Forney

You can use Magiq, a batch operation framework for LINQ.

Peter Mortensen
14.4k19 gold badges88 silver badges117 bronze badges
ivosivos

I assume you want to change values inside a query so you could write a function for it

The game featured all of Nascar’s 23 pieces that will be updated later by the community mod games. Nr2003 torrent.

But not shure if this is what you mean.

StormenetStormenet

Vb Net Linq

16.4k7 gold badges49 silver badges64 bronze badges

Suppose we have data like below,

and if we want to modify the list and replace the existing values of the list to modified values, then first create a new empty list, then loop through data list by invoking modifying method on each list item,

Vishwa GVishwa G

Not the answer you're looking for? Browse other questions tagged c#.netlinqforeach or ask your own question.

Active2 years, 1 month ago

I had a list

where Myclass is

items in li looks like

How How To Install Greasemonkey And Userscripts In Firefox And Chrome Greasemonkey For Firefox. Greasemonkey is available as an add-on for Firefox. Tampermonkey For Chrome. There’s no Greasemonkey for Chrome but as scripting enthusiasts will tell. Best Places To Find User Scripts. The script I want isn't found on userscripts.org but it is on the web, I had this one before i had to reinstall firefox. Ugoira to gif. I went to the page I found with the script with firefox, and nothing asked me if I want to install the script. Greasemonkey Manual:Installing Scripts. Installation of a script is most often done by clicking a link on a web page. Any file that ends in.user.js is a user script. When navigating to a URL which ends with.user.js, Greasemonkey will trigger the installation dialog. Note that Greasemonkey must be. Click the install button to download the script to your computer. Click on the 'Customize Google Chrome Button' to the right of the address bar and go to Tools > Extensions; Drag the downloaded script onto the Extensions page; Customize and install the script; Using Tampermonkey; Go to Chrome's Webstore and install Tampermonkey; Navigate to the Script's page that you want to install. Click on the green install. Sep 08, 2018  Download Greasemonkey for Firefox. Customize the way a web page displays or behaves, by using small bits of JavaScript.

and want result which looks like this

my code gives error can you please tell how i do this

Co. AdenCo. Aden
2,5019 gold badges42 silver badges76 bronze badges

7 Answers

Kamil BudziewskiKamil Budziewski
18.9k12 gold badges68 silver badges89 bronze badges
BaldrickBaldrick
10.6k2 gold badges23 silver badges29 bronze badges
sivabalansivabalan

If you really want to use linq, you can do something like this

or

This assumes that there are only 3 types of name. I would externalize that part into a function to make it more manageable.

SmeegsSmeegs
7,4694 gold badges28 silver badges65 bronze badges

Try this:

All values are updated in one line of code and you browse the List only ONE time. You have also a way to set a default value.

David DonariDavid Donari
SantoshSantosh
1,7605 gold badges24 silver badges38 bronze badges
FindOut_Quran

Update List Using Linq

FindOut_QuranLinq
4622 gold badges6 silver badges24 bronze badges

101 Linq Samples Vb

Not the answer you're looking for? Browse other questions tagged c#linqlist or ask your own question.

Comments are closed.