比较2个列表之间的值并减去常见元素的值-C# [英] Comparing values between 2 lists and subtracting values for common elements - C#
问题描述
我需要准备一张图表,其中我必须显示3条线.一个用于显示一周的新问题,第二用于显示一周的未完成问题,第三用于显示一周的总未解决问题.因此,我准备了一个查询,并能够成功创建2个单独的列表-一个列表维护每周新问题的数量,第二个列表维护每周封闭的问题的数量.
I need to prepare a chart wherein I'm required to show 3 lines. One for showing new issues for a week, second for closed issues for a week and third for total open issues for a week. For this reason, I have prepared a query and was able to create 2 separate lists successfully - one list maintains weekly count of new issues and second list maintains weekly count of closed issues.
以下是第一个列表的示例数据(其中包含新问题):
Here is the sample data for first list (one which maintains new issues) :
[0]: { Week = {6/14/2015 12:00:00 AM}, Count = 1 }
[1]: { Week = {3/5/2017 12:00:00 AM}, Count = 1 }
[2]: { Week = {5/21/2017 12:00:00 AM}, Count = 4 }
[3]: { Week = {6/4/2017 12:00:00 AM}, Count = 7 }
[4]: { Week = {6/11/2017 12:00:00 AM}, Count = 4 }
[5]: { Week = {6/25/2017 12:00:00 AM}, Count = 7 }
[6]: { Week = {7/9/2017 12:00:00 AM}, Count = 3 }
从以上数据中,我可以得出特定一周未解决问题的总数.
From the above data I get for total count of open issues for a particular week.
注意:对于这两个列表,星期"值都包含星期天的日期.因为我需要从星期一开始的一周,才能在图表中显示数据.
Note: For both these lists the Week values contain date which falls on Sunday. As I need the week to start from Monday while displaying data in the chart.
类似地,针对第二个列表的样本数据(一个用于维护已解决问题的列表):
Similarly for sample data for second list (one which maintains closed issues) :
[0]: { Week = {12/13/2015 12:00:00 AM}, Count = 1 }
[1]: { Week = {7/9/2017 12:00:00 AM}, Count = 3 }
[2]: { Week = {6/18/2017 12:00:00 AM}, Count = 2 }
[3]: { Week = {7/23/2017 12:00:00 AM}, Count = 8 }
[4]: { Week = {10/1/2017 12:00:00 AM}, Count = 6 }
[5]: { Week = {8/6/2017 12:00:00 AM}, Count = 3 }
[6]: { Week = {9/17/2017 12:00:00 AM}, Count = 5 }
从以上数据中,我可以得出特定一周内已解决问题的总数.
From the above data I get for total count of closed issues for a particular week.
这是这些列表的代码:
var openIssuesList = getDetails.Where(x => x.ChangedTo == "Open").Select(x => new { Week = x.Date.AddDays(x.Date.DayOfWeek == DayOfWeek.Sunday ? 0 : 7 - (int)x.Date.DayOfWeek).Date, Detail = x }).GroupBy(x => x.Week).Select(x => new { Week = x.Key, Count = x.Count() }).ToList();
var closedIssuesList = getDetails.Where(x => x.ChangedTo == "Closed").Select(x => new { Week = x.Date.AddDays(x.Date.DayOfWeek == DayOfWeek.Sunday ? 0 : 7 - (int)x.Date.DayOfWeek).Date, Detail = x }).GroupBy(x => x.Week).Select(x => new { Week = x.Key, Count = x.Count() }).ToList();
现在剩下的最后一件事情是,使用这两个列表中的值创建一个新列表,其中的值应包含一周内所有未解决问题的数据.
Now the final piece that remains is to create a new list by using the values from these 2 lists which should contain data for total open issues for a week.
说明:
- 我需要比较上面两个列表中的星期值.
- 如果周值相等,则计算之间的差 来自两个列表的特定星期的计数值.
- 在新列表中保存星期"值和计数值(计算出差异后).
- 如果周值不匹配,则选择此类值(周"和周" 计数)并按原样存储在此新列表中.
- I need to compare the week values from the above 2 lists.
- If the week values are equal, then calculate the difference between the Count values of those particular week from both lists.
- Save the Week value and count value (after calculating the difference) in this new list.
- If week values do not match then pick such values (both Week and Count) and store them as is in this new list.
因此,从上面提供的示例数据中,这是新列表的外观:
So from the above provided sample data here's how the new list should like :
[0]: { Week = {6/14/2015 12:00:00 AM}, Count = 1 } // As is value from first list - openIssuesList
[1]: { Week = {12/13/2015 12:00:00 AM}, Count = 1 } // As is value from second list - closedIssuesList
[2]: { Week = {3/5/2017 12:00:00 AM}, Count = 1 } // As is value from first list - openIssuesList
[3]: { Week = {5/21/2017 12:00:00 AM}, Count = 4 } // As is value from first list - openIssuesList
[4]: { Week = {6/4/2017 12:00:00 AM}, Count = 7 } // As is value from first list - openIssuesList
[5]: { Week = {6/11/2017 12:00:00 AM}, Count = 4 } // As is value from first list - openIssuesList
[6]: { Week = {6/18/2017 12:00:00 AM}, Count = 2 } // As is value from second list - closedIssuesList
[7]: { Week = {6/25/2017 12:00:00 AM}, Count = 7 } // As is value from first list - openIssuesList
[8]: { Week = {7/9/2017 12:00:00 AM}, Count = 0 } // These is common week from both lists. Hence we calculate the difference between count values. So 3-3 = 0.
[9]: { Week = {7/23/2017 12:00:00 AM}, Count = 8 } // As is value from second list - closedIssuesList
[10]: { Week = {8/6/2017 12:00:00 AM}, Count = 3 } // As is value from second list - closedIssuesList
[11]: { Week = {9/17/2017 12:00:00 AM}, Count = 5 } // As is value from second list - closedIssuesList
[12]: { Week = {10/1/2017 12:00:00 AM}, Count = 6 } // As is value from second list - closedIssuesList
根据以上数据,请参阅此列表的第8个元素.此列表7/9/2017中的星期在openIssuesList(第6个元素)和closeIssuesList(第2个元素)中很常见
From the above data kindly see the 8th element of this list. The week in this list 7/9/2017 was common from both the openIssuesList (6th element) and closedIssuesList (2nd element)
实现此列表的代码是什么?
What would be the code to achieve this list?
注意:我已经从这些列表中的所有DateTime值中删除了代码中的Time元素值.因此,所有日期值都会在这些列表中显示为12:00:00 AM.
Note: I have remove the Time element value in my code from all the DateTime values in these lists. Hence all the date values appear with 12:00:00 AM in these lists.
推荐答案
如果您绝对不需要LINQ解决方案,则可以创建一个帮助器类
If you don't absolutely need a LINQ solution, you can create a helper class
public class WeekCount
{
public DateTime Week { get; set; }
public int Count { get; set; }
}
,使用此类修改您的选择
, modify your selects using this class
.Select(x => new WeekCount { Week = x.Key, Count = x.Count() })
,然后简单地进行操作:
, and then do it simply:
var totalIssuesList = openIssuesList.ToList();
foreach (var closedWeekCount in closedIssuesList)
{
var totalWeekCount = totalIssuesList.FirstOrDefault(owc => owc.Week == closedWeekCount.Week);
if (totalWeekCount != null)
{
totalWeekCount.Count = totalWeekCount.Count - closedWeekCount.Count;
}
else
{
totalIssuesList.Add(closedWeekCount);
}
}
totalIssuesList = totalIssuesList.OrderBy(twc => twc.Week).ToList();
这篇关于比较2个列表之间的值并减去常见元素的值-C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!