Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# list join

作者:渚寒烟淡   发布日期:2026-03-15   浏览:55

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static void Main()
    {
        // 定义两个列表
        List<int> list1 = new List<int> { 1, 2, 3, 4 };
        List<string> list2 = new List<string> { "one", "two", "three", "four" };

        // 使用 Join 方法连接两个列表
        var joinedList = list1.Join(
            list2, 
            id => id, 
            str => list1.IndexOf(list1.FirstOrDefault(x => x.ToString() == str)) + 1, 
            (id, str) => new { ID = id, StringValue = str }
        );

        // 输出连接后的结果
        foreach (var item in joinedList)
        {
            Console.WriteLine($"ID: {item.ID}, StringValue: {item.StringValue}");
        }
    }
}

解释说明:

  • list1list2 是两个示例列表,分别包含整数和字符串。
  • Join 方法用于将两个列表基于某个键进行连接。在这个例子中,我们尝试通过索引将两个列表中的元素关联起来。
  • id => id 表示使用 list1 中的元素作为键。
  • str => list1.IndexOf(list1.FirstOrDefault(x => x.ToString() == str)) + 1 尝试找到 list2 中字符串对应的 list1 索引。
  • (id, str) => new { ID = id, StringValue = str } 指定如何组合匹配的元素,生成一个新的匿名对象。

请注意,这个例子假设 list1list2 的元素有某种对应关系(例如,数字和它们的英文表示)。如果实际应用中没有这种直接的映射关系,可能需要根据具体需求调整连接逻辑。

上一篇:c# sort

下一篇:c# byte[]

大家都在看

c# 二进制

c# 创建目录

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站