[英]Add missing values to a list in xamarin forms
I have a custom object list which has month and another int value. 我有一个自定义对象列表,其中包含月份和另一个int值。 I am getting this list from the database.
我从数据库中获取此列表。 So data coming from a web service as JSON.
因此,来自Web服务的数据为JSON。 Sometimes month is not consecutive.
有时月份不是连续的。 Sometimes some month get missing.
有时一些月会失踪。 For example if this is month 7,
例如,如果这是第7个月,
then months in list may contains something like this. 然后列表中的月份可能包含这样的内容。 {1,2,3,6,7}
{1,2,3,6,7}
so I want to add the missing months which are 4,5 ---- {1,2,3,4,5,6,7} 所以我想补充4,5的遗漏月份---- {1,2,3,4,5,6,7}
other value should be 0 (NEW_REC_COUNT) 其他值应为0(NEW_REC_COUNT)
My Object class 我的对象类
public class NewData
{
public int MONTH { get; set; }
public int NEW_REC_COUNT { get; set; }
}
Sample Json 示例Json
[
{
"MONTH": 1,
"NEW_REC_COUNT": 19
},
{
"MONTH": 2,
"NEW_REC_COUNT": 5
},
{
"MONTH": 3,
"NEW_REC_COUNT": 2
},
{
"MONTH": 6,
"NEW_REC_COUNT": 9
},
{
"MONTH": 7,
"NEW_REC_COUNT": 3
}
]
You can try below approach, 您可以尝试以下方法,
Select all months (int value) from list using Select
使用
Select
从列表中选择所有月份(int值)
var months = NewDataList.Select(x => x.MONTH); // This will give you all integers ie MONTHs.
Find Max()
from months and create Range from 1... maxMonths
从几个月中查找
Max()
并从1... maxMonths
创建范围
var maxMonths = months.Max(); var oneTomaxMonths = Enumerable.Range(1,maxMonths).ToList();
Now you have 2 lists ie months
and oneToMaxMonths
, use Except to get missing Months from list of New Data 现在你已经2所列出,即
months
和oneToMaxMonths
,使用除来自新数据的清单得到失踪月
var results = oneTomaxMonths.Except(months);
Foreach result create new instance with NEW_REC_COUNT = 0
Foreach结果使用
NEW_REC_COUNT = 0
创建新实例
POC : .net Fiddle POC: .net小提琴
If you don't have a lot of data, you can try a smiple loop and Insert
omitted items into the list: 如果没有大量的数据,你可以尝试smiple 环和
Insert
省略的项到列表:
List<NewData> list = ...
// If list is not guarantee to be sorted
list.Sort((a, b) => a.MONTH.CompareTo(b.MONTH));
for (int i = 0; i < list.Count - 1; ++i) {
NewData current = list[i];
NewData next = list[i + 1];
// Do we have a hole at position i + 1?
if (current.MONTH + 1 < next.MONTH) {
list.Insert(i + 1, new NewData() {
MONTH = current.MONTH + 1, // Omitted month
NEW_REC_COUNT = 0, // Default value
});
}
}
Edit: If we want months from 1
up and including the current month ( DateTime.Today.Month
), we can use Linq : 编辑:如果我们想要从
1
开始的月份,包括当前月份( DateTime.Today.Month
),我们可以使用Linq :
using System.Linq;
...
List<NewData> list = ...
// Let's generalize a bit if you want, say Q3 period
int fromMonth = 1;
int upToMonth = DateTime.Today.Month; // or 7 for testing
list = Enumerable
.Range(fromMonth, upToMonth - fromMonth + 1)
.Select(month =>
list.FirstOrDefault(item => item.MONTH == month)
?? new NewData() { MONTH = month, // Omitted month
NEW_REC_COUNT = 0 }) // Default value
.ToList();
If you want to modify existing list
: 如果要修改现有
list
:
list.AddRange(Enumerable
.Range(fromMonth, upToMonth - fromMonth + 1)
.Where(month => !list.Any(item => item.MONTH == month))
.Select(month => new NewData() {
MONTH = month,
NEW_REC_COUNT = 0 })
.ToArray());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.