7

我正在尝试在 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
4

5 回答 5

15
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
             };
于 2013-02-21T16:19:14.333 回答
2

尝试这个

foreach(var f in first)
{
    foreach(var s in second)
    {
        if(f.ID == s.ID)
        {
            f.fAttr = item.itemAtrr;
        }
    }
}
于 2013-02-21T16:33:23.567 回答
0

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

于 2013-02-22T15:46:44.460 回答
0

您可以在 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 
             };
于 2013-02-21T16:24:00.493 回答
0
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; }
        }
    }
}
于 2013-02-21T16:29:46.927 回答