【问题标题】:Join two list on a specified column在指定列上加入两个列表
【发布时间】:2013-02-21 16:13:23
【问题描述】:

我正在尝试在 ID 列上加入两个列表(flist 和 slist)。列表定义、类定义、列表内容和所需结果显示在下方。

List<first> flist= new List<first>();
List<second> slist= new List<second>();


public class first
{
   public string name { get; set; }
   public int ID{ get; set; }
   public string itemAttr { get; set; }
}
public class second
{
   public int ID{ get; set; }
   public string itemAttr{ get; set; }
}

列出内容

flist:
apples | 1
bananas| 2
trees  | 3

slist:
1      | fruit
3      | not-fruit

想要的结果:

flist:
apples   |   1     | fruit
bananas  |   2     |
trees    |   3     | not-fruit

【问题讨论】:

标签: c# list


【解决方案1】:
List<first> flist= new List<first>();
List<second> slist= new List<second>();

var result = from f in flist
             join s in slist on f.ID equals s.ID into g
             select new {
                 f.name,
                 f.ID,
                 itemAttr = g.Any() ? g.First().itemAttr : null
             };

【讨论】:

  • 这是完美的。对于任何有相同问题的人,您可以使用 foreach(var v in result) 访问结果。
【解决方案2】:

试试这个

foreach(var f in first)
{
    foreach(var s in second)
    {
        if(f.ID == s.ID)
        {
            f.fAttr = item.itemAtrr;
        }
    }
}

【讨论】:

    【解决方案3】:

    您可以在 Linq 中使用“左外”连接:

    var joined = from l1 in flist
                 join l2 in slist on l1.ID equals l2.ID into gj
                 from l2_sub in gj.DefaultIfEmpty()
                 select new { 
                     name = l1.name, 
                     ID = l1.ID, 
                     itemAttr = l2_sub == null ? String.Empty : l2_sub.itemAttr 
                 };
    

    【讨论】:

      【解决方案4】:
      namespace WindowsFormsApplication26
      {
          public partial class Form1 : Form
          {
              public Form1()
              {
                  InitializeComponent();
              }
      
      
              private void Form1_Load(object sender, EventArgs e)
              {
                  List<first> flist = new List<first>();
                  List<second> slist = new List<second>();
      
                  flist.Add(new first("apples", 1));
                  flist.Add(new first("bananas", 2));
                  flist.Add(new first("trees", 3));
      
                  slist.Add(new second(1, "Fruit"));
                  slist.Add(new second(2, ""));
                  slist.Add(new second(3, "Not-Fruit"));
      
                  var result = (from t in flist
                                join x in slist
                                on t.ID equals x.ID
      
                                select new
                                {
                                    Name = t.name,
                                    Id = t.ID,
                                    attrib = x.itemAttr
      
                                }).ToList();
              }
      
      
              public class first
              {
                  public first()
                  {
      
                  }
      
                  public first(string n, int id)
                  {
                      name = n;
                      ID = id;
                  }
      
                  public string name { get; set; }
                  public int ID { get; set; }
      
              }
      
              public class second
              {
                  public second()
                  {
      
                  }
      
                  public second(int id, string itm)
                  {
                      ID = id;
                      itemAttr = itm;
                  }
      
                  public int ID { get; set; }
                  public string itemAttr { get; set; }
              }
          }
      }
      

      【讨论】:

        【解决方案5】:

        有什么问题?创建一个新类,其中第三个带有参数:ID、名称、itemAttr。使用该类创建新列表,并将写入所需位置所需的数据。

        【讨论】:

          猜你喜欢
          • 2013-07-07
          • 1970-01-01
          • 1970-01-01
          • 2011-07-26
          • 2012-05-29
          • 1970-01-01
          • 2012-03-12
          • 1970-01-01
          • 2022-12-24
          相关资源
          最近更新 更多