English 中文(简体)
C# Update a datatable from another datatable with LINQ
原标题:C# Update a datatable from another datatable with LINQ
  • 时间:2023-09-09 02:22:31
  •  标签:
  • c#

在我的C#代码中,我有两个数据表:

inventory_table

SKU Title Qty
SAM-GAL-S7-32 Samsung Galaxy S7 128GB 5
SAM-GAL-S6-64 Samsung Galaxy S6 64GB 10
IP-13P-128 Apple iPhone 13 Pro 128GB 9
IP-12-64 Apple iPhone 12 64GB 6
LG-V60-128 LG V60 128GB 3

页: 1

SKU Qty
SAM-GAL-S6-64 2
IP-13P-128 1
LG-V60-128 1

I want to substract the quantity of 页: 1 from the inventory_table for the matching SKU, so the result should be:

inventory_table (updated)

SKU Qty
SAM-GAL-S7-32 5
SAM-GAL-S6-64 8
IP-13P-128 8
IP-12-64 6
LG-V60-128 2

The rows with SKU "SAM-GAL-S6-64", "IP-13P-128" and "LG-V60-128" now have updated quantity. These tables only exists in memory, not in database, and the result table will be exported to a text file. In reality, inventory_table would have a few thousand rows while 页: 1 has 20-30 rows. I believe this task can be done with LINQ. Please advise. Thanks

无。 我需要咨询

问题回答

• 检查以下编码:

using System;
using System.Data;
using System.Linq;

class Program
{
    static void Main()
    {
        // Create sample DataTables
        DataTable inventoryTable = new DataTable();
        inventoryTable.Columns.Add("SKU", typeof(string));
        inventoryTable.Columns.Add("Qty", typeof(int));
        
        DataTable orderTable = new DataTable();
        orderTable.Columns.Add("SKU", typeof(string));
        orderTable.Columns.Add("Qty", typeof(int));
        
        // Populate sample data (replace with your data)
        inventoryTable.Rows.Add("SAM-GAL-S7-32", 5);
        inventoryTable.Rows.Add("SAM-GAL-S6-64", 10);
        inventoryTable.Rows.Add("IP-13P-128", 9);
        inventoryTable.Rows.Add("IP-12-64", 6);
        inventoryTable.Rows.Add("LG-V60-128", 3);
        
        orderTable.Rows.Add("SAM-GAL-S6-64", 2);
        orderTable.Rows.Add("IP-13P-128", 1);
        orderTable.Rows.Add("LG-V60-128", 1);

        // Subtract quantities using LINQ
        var result = from inventoryRow in inventoryTable.AsEnumerable()
                     join orderRow in orderTable.AsEnumerable()
                     on inventoryRow.Field<string>("SKU") equals orderRow.Field<string>("SKU")
                     select new
                     {
                         SKU = inventoryRow.Field<string>("SKU"),
                         Qty = inventoryRow.Field<int>("Qty") - orderRow.Field<int>("Qty")
                     };

        // Update the inventoryTable with the new quantities
        foreach (var item in result)
        {
            DataRow row = inventoryTable.AsEnumerable()
                                        .Where(r => r.Field<string>("SKU") == item.SKU)
                                        .FirstOrDefault();
            if (row != null)
            {
                row.SetField("Qty", item.Qty);
            }
        }

        // Display the updated inventoryTable
        Console.WriteLine("Updated inventory_table:");
        foreach (DataRow row in inventoryTable.Rows)
        {
            Console.WriteLine($"{row["SKU"]}	{row["Qty"]}");
        }
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签