C# 如何将缺少的数字添加到列表中
我有一个数组列表。其中包含以逗号分隔的月份号和数据。 现在,我想循环浏览这个列表,检查是否缺少任何月份号。如果需要,则需要以数据部分为零的顺序添加该月数C# 如何将缺少的数字添加到列表中,c#,asp.net,list,C#,Asp.net,List,我有一个数组列表。其中包含以逗号分隔的月份号和数据。 现在,我想循环浏览这个列表,检查是否缺少任何月份号。如果需要,则需要以数据部分为零的顺序添加该月数 List<string> MyData = new List<string>(); MyData.Add("4,500"); //dynamically adding. 4(april) and 6(june) are month numbers. MyData.Add("6,400"); foreach
List<string> MyData = new List<string>();
MyData.Add("4,500"); //dynamically adding. 4(april) and 6(june) are month numbers.
MyData.Add("6,400");
foreach (string str in MyData)
{
int GetmonthNum = Convert.ToInt32(str.Substring(0, str.IndexOf(',')));
}
您可以像这样使用
contains
var result = new List<string>();
for (int i = 1; i <= 12 ; i++)
{
var firstMatch = myData.FirstOrDefault(x => x.Contains(i + ","));
if (firstMatch == null)
{
result.Add(i + ",0");
}
else
{
result.Add(firstMatch);
}
// or short code: result.Add(firstMatch ?? i + ",0" );
}
var result=newlist();
对于(int i=1;i x.包含(i+“,”);
if(firstMatch==null)
{
结果:添加(i+”,0“);
}
其他的
{
结果。添加(firstMatch);
}
//或短代码:result.Add(firstMatch??i+“,0”);
}
如果你想把“4500”作为第一项,那就试试吧
var minMonth = myData.Min(x => Convert.ToInt32(x.Substring(0, x.IndexOf(",", StringComparison.CurrentCulture))));
var result = new List<string>();
for (int i = minMonth - 1; i < minMonth + 11; i++)
{
var firstMatch = myData.FirstOrDefault(x => x.StartsWith((i % 12) + 1 + ","));
result.Add(firstMatch ?? (i % 12) + 1 + ",0");
}
var minMonth=myData.Min(x=>Convert.ToInt32(x.Substring(0,x.IndexOf(“,”,StringComparison.CurrentCulture));
var result=新列表();
对于(int i=minMonth-1;ix.StartsWith((i%12)+1+“,”);
结果。添加(第一个匹配???(i%12)+1+”,0“);
}
如果您的列表中有12项[对应于12个月],请尝试下面的代码
List<string> MyData = new List<string>();
MyData.Add("4,500"); //dynamically adding. 4(april) and 6(june) are month numbers.
MyData.Add("6,400");
int i = 1;
// use variable i from 1 to 12 as month indicator
foreach (string str in MyData)
{
string month = str.Substring(0, str.IndexOf(','));
// get the month from your list item here
int GetmonthNum = Convert.ToInt32( month==string.Empty || month==null ? i.ToString() : month );
// here use conditional operator to check if month is not there in list item , if it is not present than return i as misisng month
i++;
}
List MyData=new List();
MyData.Add(“4500”)//动态添加。4(4月)和6(6月)是月数。
MyData.Add(“6400”);
int i=1;
//使用1到12之间的变量i作为月份指示器
foreach(MyData中的字符串str)
{
string month=str.Substring(0,str.IndexOf(',');
//从您的列表项中获取月份
int GetmonthNum=Convert.ToInt32(month==string.Empty | | | month==null?i.ToString():month);
//这里使用条件运算符检查列表项中是否不存在月份,如果不存在,则返回i作为misng month
i++;
}
如果您在编写代码时遇到任何问题,请告诉我。我可以通过以下循环实现您想要的结果。但是有很多方法可以做到这一点
int arrayIndex = 0;
int month = 1;
for (int i = 0; i < 12; i++)
{
if (myArray[arrayIndex].Split(',')[0] == Convert.ToString(month))
{
MyData.Add(myArray[arrayIndex]);
month++;
arrayIndex++;
}
else
{
MyData.Add(Convert.ToString(month) + ",0");
month++;
}
}
int-arrayIndex=0;
整月=1;
对于(int i=0;i<12;i++)
{
if(myArray[arrayIndex].Split(',')[0]==Convert.ToString(月))
{
Add(myArray[arrayIndex]);
月份++;
arrayIndex++;
}
其他的
{
MyData.Add(Convert.ToString(month)+“0”);
月份++;
}
}
List MyData=new List();
MyData.Add(“4500”);
MyData.Add(“6400”);
var月数=可枚举范围(1,12);
foreach(整数月,以月为单位)
{
if(MyData.Any(a=>a.Split(',')[0]==month.ToString())
继续;
Add(string.Format(“{0},{1}”,month.ToString(),“0”);
}
这项工作:
MyData =
MyData
.Select(x => x.Split(',').Select(y => int.Parse(y)).ToArray())
.Concat(Enumerable.Range(1, 12).Select(x => new [] { x, 0 }).ToArray())
.OrderBy(x => x[0])
.GroupBy(x => x[0], x => x[1])
.SelectMany(x => x.Take(1), (y, z) => $"{y.Key},{z}")
.ToList();
这就产生了:
1,0
2,0
3,0
4,500
5,0
6,400
7,0
8,0
9,0
10,0
11,0
12,0
1,0
2,0
3,0
4,500
5,0
6,400
7,0
8,0
9,0
10,0
11,0
12,0
是否有任何方法可以获得“4500”、“5,0”、“6400”、“7,0”、“8,0”、“9,0”、“10,0”、“11,0”、“12,0”、“1,0”、“2,0”、“3,0”格式的结果输出?是否将列表转换为字符串或将“4500”设置为第一项?是否将第一项设置为其本身。然后依次重复其他月份。例:如果第一项是四月。那么新的数组列表应该是从四月、五月、六月,。。。march@user2431727当minMonth被指定为2(二月)时,它工作不正常。2月、3月、4月、5月、4月、7月、5月、9月、10月、11月、12月、1月你有什么问题?minMonth=2,则二月为第一项。或者你想要什么?
MyData =
MyData
.Select(x => x.Split(',').Select(y => int.Parse(y)).ToArray())
.Concat(Enumerable.Range(1, 12).Select(x => new [] { x, 0 }).ToArray())
.OrderBy(x => x[0])
.GroupBy(x => x[0], x => x[1])
.SelectMany(x => x.Take(1), (y, z) => $"{y.Key},{z}")
.ToList();
1,0
2,0
3,0
4,500
5,0
6,400
7,0
8,0
9,0
10,0
11,0
12,0