c#linq增加新的一条记录同时删除最后一条代码
时间: 2023-12-08 13:03:08 浏览: 138
一个B样条C#程序源代码
4星 · 用户满意度95%
Sure! Here's an example of how you can use LINQ in C# to add a new record to a collection and remove the last record:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
List<string> records = new List<string>() { "Record1", "Record2", "Record3" };
// Add a new record
string newRecord = "Record4";
records.Add(newRecord);
// Remove the last record
string lastRecord = records.Last();
records.Remove(lastRecord);
// Print the updated records
foreach (var record in records)
{
Console.WriteLine(record);
}
}
}
```
In this example, we have a `List<string>` called `records` that initially contains three records. We use the `Add` method to add a new record ("Record4") to the list. Then, we use the `Last` method to retrieve the last record in the list, and the `Remove` method to remove it. Finally, we print the updated records using a foreach loop.
Note that this example assumes that you have already imported the necessary namespaces (`System` and `System.Collections.Generic`) and have a basic understanding of C# and LINQ.
阅读全文