Update List Using Linq In Vb.net
Writing a LINQ to DataTable that updates values. Data Platform Development > ADO.NET DataSet. Being a VB.NET developer, this is how I could code this situation without LINQ: For Each drTest As DataRow In mdtInspectionTests.Rows drTest(InspectionTest.Select) = False Next For Each drTemplateTest As DataRow In. Update (Edit) XML Data using DataSet and DataRow with LINQ in Asp.Net C# - Vb.Net. Update (Edit) XML Data using DataSet and DataRow with LINQ in Asp.Net C# - Vb.Net.
Introduction
With the arrival of LINQ, sometimes it is more useful to have all the database values loaded in a generic list instead of being in DataTables. This is because the use of a list is more friendly in LINQ, in my opinion. This article shows how to load any DataTable in any generic list in a dynamic way. We are going to create two classes, fill two DataTables with a query, and load all the values within the DataTables in two different generic lists. At the end, we will see a dummy example of LINQ 'navigation' through the list.
Background
In order to use the code, it's necessary to have two important things in mind:
- You must define a class with properties names as the same as the query fields.
- The data types of the properties must be the same as that of the data fields.
Using the code
First of all, we are going to create two tables and populate them. In this example, they are created in a SQL Server database, but feel free to use any kind of database:
Once the tables are created and populated, we have to declare two classes. These classes will be a representation of the row of the table we want to load in the list, or at least a representation of the fields requested in the query:
As you can see, the names of the properties and their types are identical to the fields declared in the tables.
Now, we are going to check the 'core' class. The class DataFiller
is a Generic class. The type of this class will be the same as the 'register like' class. It will receive a DataTable
, and navigate thought the DataRow
s and the fields. When the loop reaches each field, it will locate the corresponding member of the class and will assign the correct value inside the property.
Now, we can run the test example:
You can download a test project in order to check how it works. Feel free to leave any comments or suggestions. Hope it helps.
I am a bit new to linq, I am only using linq for filtering the data. Now, I want to write a query for the below:
Con.Numbers is a dictionary, but now I converted it into a list, hence the above code wont work with list, could you please tell me how I can achieve it with Linq is the Con.NUmbers is a list. Thank you.
Additional Info:Class Structure is :
2 Answers
Don't know if I have misunderstood you somewhere.
Not sure why you want to use LINQ specifically. This is perfectly clear:
If for some reason you want LINQ-like syntax, you could use the List(T).ForEach
:
Linq Select Vb
For course, this is not 'real' LINQ, but again, I'm not sure why it would matter.
If you are really forced (?) to use LINQ, you might do:
Vb Linq Query
But of course the code is nonsense. Don't do this -- stick to what is clear and obvious, which in this case means just looping over the list.
EDIT
Fixed terrible misuse of Function
Vb Linq Examples
LINQ stands for Language Integrated Query. Query means retrieving data, so it's not so good for updating data. You maybe able to use it to retrieve another copy of the data that is updated, but not for directly updating the original.
If you want to make a new list with the data updated, you might do something like this:
BlueMonkMN