根据匹配条件从5个列表中查找匹配项

时间:2018-10-20 06:58:53

标签: c# list

鉴于我有一个看起来像这样的模型:

	<div class="sitecontainer">
		
		<div class="tips-and-news-wrap left-containrer">

			<ul class="tips-and-news-posts">

				<li>1st Post</li>
				<li>2nd Post</li>
				<li>3rd one</li>
	
			</ul>

		</div>
					
		<div class="tips-and-news-wrap right-conatiner">

				<ul class="tips-and-news-posts">

					<li>4th post</li>
					<li>5th post</li>
					<li>6th post</li>

				</ul>

		</div>

	</div

我有一些初始化public sealed class BaseInfo { public string ValueToBeMatched { get; set; } public string CreatedValue { get; set; } } 类的变量,如下所示:

BaseInfo

条件是5个列表必须相互匹配2个或更多匹配,然后它将结果存储到新列表中。

我正在考虑对每个列表使用for循环,并使每个循环中的if语句与列表的每个索引的条件匹配。但是我被困住了。

这是我坚持的代码:

List<BaseInfo> zeroBaseInfo = new List<BaseInfo>
{
   new BaseInfo
   {
      ValueToBeMatched = "001",
      CreatedValue = "2008-10-10"
   },
   new BaseInfo
   {
      ValueToBeMatched = "003",
      CreatedValue = "2007-10-10"
   },
}

List<BaseInfo> firstBaseInfo = new List<BaseInfo>
{
   new BaseInfo
   {
      ValueToBeMatched = "002",
      CreatedValue = "2006-10-10"
   },
   new BaseInfo
   {
      ValueToBeMatched = "001",
      CreatedValue = "2008-10-10"
   },
}

List<BaseInfo> secondBaseInfo= new List<BaseInfo>
{
   new BaseInfo
   {
      ValueToBeMatched = "001",
      CreatedValue = "2008-10-10"
   },
   new BaseInfo
   {
      ValueToBeMatched = "002",
      CreatedValue = "2006-10-10"
   },
}

List<BaseInfo> thirdBaseInfo = new List<BaseInfo>
{
   new BaseInfo
   {
      ValueToBeMatched = "002",
      CreatedValue = "2006-10-10"
   },
   new BaseInfo
   {
      ValueToBeMatched = "001",
      CreatedValue = "2008-10-10"
   },
}

List<BaseInfo> fourthBaseInfo = new List<BaseInfo>
{
   new BaseInfo
   {
      ValueToBeMatched = "",
      CreatedValue = DateTime.Now.ToString("yyyy-MM-dd")
   },
   new BaseInfo
   {
      ValueToBeMatched = "001",
      CreatedValue = "2008-10-10"
   },
}

编辑:

预期输出:

for (int i = 0; i < zeroBaseInfo.Count; i++)
            {
                for (int j = 0; j < firstBaseInfo.Count; j++)
                {
                    for (int k = 0; k < secondBaseInfo.Count; k++)
                    {
                        for (int o = 0; o < thirdBaseInfo.Count; o++)
                        {
                            for (int p = 0; p < fourthBaseInfo.Count; p++)
                            {
                                // I am stuck in this point to what needs to be compare, as if statement only works against comparison value between 1 to 1 and not all of lists.
                            }
                        }
                    }
                }
            }

List<BaseInfo> matchedNewLists = new List<BaseInfo> { new BaseInfo { ValueToBeMatched = "001", CreatedValue = "2006-10-10" }, new BaseInfo { ValueToBeMatched = "002", CreatedValue = "2008-10-10" } } 001被满足以来,以上5个列表中的条件(等于2个匹配项或更多)

任何帮助和您的回答都非常感激。

非常感谢!

2 个答案:

答案 0 :(得分:0)

这可以通过一些LINQ操作来完成。我将它们分开,以弄清楚我在这里做什么:

var oneLongList = new[] { zeroBaseInfo, firstBaseInfo, secondBaseInfo, thirdBaseInfo, fourthBaseInfo }.SelectMany(x => x);
var allValuesToBeMatched = oneLongList.Select(x => x.ValueToBeMatched);
var grouped = allValuesToBeMatched.GroupBy(x => x);
var filtered = grouped.Where(x => x.Count() >= 2).Select(x => x.Key);
var finalResult = filtered.Select(x => new BaseInfo { ValueToBeMatched = x });

或者您可以将它们全部组合为一个:

var finalResult = new[] { zeroBaseInfo, firstBaseInfo, secondBaseInfo, thirdBaseInfo, fourthBaseInfo }
    .SelectMany(x => x)
    .Select(x => x.ValueToBeMatched)
    .GroupBy(x => x)
    .Where(x => x.Count() >= 2)
    .Select(x => new BaseInfo { ValueToBeMatched = x.Key });

编辑:

var finalResult = new[] { zeroBaseInfo, firstBaseInfo, secondBaseInfo, thirdBaseInfo, fourthBaseInfo }
    .SelectMany(x => x)
    .GroupBy(x => x.ValueToBeMatched)
    .Where(x => x.Count() >= 2)
    .Select(x => x.First());

答案 1 :(得分:0)

您可以使用linq。

List<BaseInfo> matchedNewLists = 
                zeroBaseInfo
                .Union(firstBaseInfo)
                .Union(secondBaseInfo)
                .Union(thirdBaseInfo)
                .Union(fourthBaseInfo)
                .GroupBy(bi => new { bi.ValueToBeMatched, bi.CreatedValue})
                .Where(grp => grp.Count() > 1)
                .Select(grp => 
                        new BaseInfo()
                        {
                            ValueToBeMatched = grp.Key.ValueToBeMatched,
                            CreatedValue = grp.Key.CreatedValue
                        })
                .ToList();