如何在C#

时间:2018-11-01 23:03:08

标签: c#

我有两个列表,如何合并或合并它们。

与此类似的第一个列表:

dateList = [{01/10/2018, 02/10/2018, 03/10/2018, 04/10/2018, 05/10/2018, 06/10/2018}]

第二个与此类似的列表:

markStatus = [{01/10/2018, true}, {03/10/2018, true}, {05/10/2018, false }]

我希望结果像这样:

result = `[{01/10/2018, true}, {2/10/2018, false}, {03/10/2018, true}, {04/10/2018, false}, {05/10/2018, false}, {06/10/2018, false}]

2 个答案:

答案 0 :(得分:3)

这里必须猜测一些属性名称:

dateList.Select(d => (d, markStatus.FirstOrDefault(s => s.Date == d)?.Select(s => s.Status) ?? false));

答案 1 :(得分:1)

enter image description here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp16
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] dateList = { "01/10/2018", "02/10/2018", "03/10/2018", "04/10/2018", "05/10/2018", "06/10/2018" };
            Dictionary<string, bool> markStatus = new Dictionary<string, bool>()
            {
                {"01/10/2018",true } ,
                {"03/10/2018",true },
                {"05/10/2018",false }
            };

            Dictionary<string, bool> result = new Dictionary<string, bool>();

            bool flag;

            foreach (var item in dateList)
            {
                flag = false;
                foreach (var key in markStatus.Keys)
                {
                    if (item.Equals(key))
                    {
                        result.Add(item, markStatus[key]);
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    result.Add(item, false);
                }
            }


        }
    }

}