我正在尋找簡單的,最好是 LINQ 解決這個問題的方法。我確實寫了一個解決它的方法,但它的全尺寸自定義方法也需要排序,這可能會減慢它的速度。
public class CustomType
{
public int X {get;set;}
public int Y {get;set;}
}
示例串列:
List<CustomType> ListOfCustomTypes = new List<CustomType>();
讓我們說它包含這個:
X Y
0 1
1 2
4 3
5 4
8 4
現在我所說的“填充空白空間”是什么意思。我想將缺少的元素添加到串列中,使其看起來像這樣:
X Y
0 1
1 2
2 0
3 0
4 3
5 4
6 0
7 0
8 4
Y值無關緊要,有問題的部分是閱讀介于兩者之間的內容X。
這是我目前的解決方案:
ListOfCustomTypes = ListOfCustomTypes.OrderBy(x => x.X).ToList();
var helper = new List<CustomType>();
for (int i = 1; i < ListOfCustomTypes.Count; i ) //check whole list
{
if (ListOfCustomTypes[i].X - 1 != ListOfCustomTypes[i - 1].X) //if gap is found
{
var lowerValue = ListOfCustomTypes[i - 1].X;
var higherValue = ListOfCustomTypes[i].X;
for (int j = lowerValue 1; j < higherValue; j ) //start filling the gap
{
var temp = new CustomType();
temp.X = j;
temp.Y = 0;
helper.Add(temp);
}
}
}
ListOfCustomTypes.AddRange(helper); //add filled gaps to the list
老實說,這種方法可能很好,但我只是喜歡 LINQ,當我不得不在沒有它的情況下處理資料時,它會傷害我。
uj5u.com熱心網友回復:
您可以對所有已排序的專案提出一個簡單的回圈:
private static IEnumerable<CustomType> Fill(IEnumerable<CustomType> source) {
int expected = 0; // let compiler be happy, we'll rewrite this value
bool first = true;
foreach (var item in source.OrderBy(x => x.X)) {
// Filling the hole if any detected
if (!first && item.X > expected)
while (item.X > expected)
yield return new CustomType() { X = expected , Y = 0 };
first = false;
expected = item.X 1;
yield return item;
}
}
然后你可以放
ListOfCustomTypes = Fill(ListOfCustomTypes).ToList();
編輯:
演示。首先我們需要一些CustomType:
class CustomType {
public int X;
public int Y;
public override string ToString() => $"{X,2} : {Y,3}";
}
然后我們可以把
List<CustomType> demo = new List<CustomType>() {
new CustomType() { X = 0, Y = 0 },
new CustomType() { X = 1, Y = 10 },
new CustomType() { X = 4, Y = 40 },
new CustomType() { X = 5, Y = 50 },
new CustomType() { X = 8, Y = 80 },
new CustomType() { X = 10, Y = 100 },
new CustomType() { X = 10, Y = 101 },
new CustomType() { X = 12, Y = 120 },
};
string report = string.Join(Environment.NewLine, Fill(demo));
Console.Write(report);
結果:
0 : 0
1 : 10
2 : 0
3 : 0
4 : 40
5 : 50
6 : 0
7 : 0
8 : 80
9 : 0
10 : 100
10 : 101
11 : 0
12 : 120
uj5u.com熱心網友回復:
這不使用 LINQ,但它比您帖子中的版本簡單一點。 (在這里運行它。)
using System;
using System.Collections.Generic;
public class CustomType
{
public int X;
public int Y;
}
public class Program
{
static List<CustomType> BuildSampleList()
{
var testData = new int[]{
0, 1,
1, 2,
4, 3,
8, 2,
5, 5,
5, 6
};
var list = new List<CustomType>();
for (var i = 0; i < testData.Length; i = 2)
{
list.Add(new CustomType { X=testData[i], Y=testData[i 1] });
}
return list;
}
static void print(IEnumerable<CustomType> list)
{
foreach (var item in list)
{
Console.WriteLine($"{item.X} {item.Y}");
}
}
static IEnumerable<CustomType> FillHoles(List<CustomType> list)
{
var key = 0;
var i = 0;
// in-place sort
list.Sort((a,b) => a.X.CompareTo(b.X));
while (i < list.Count)
{
if (key > list[i].X)
{
yield return list[i];
i;
}
else if (key < list[i].X)
{
yield return new CustomType { X=key, Y=0 };
key;
}
else
key;
}
}
public static void Main()
{
var inputList = BuildSampleList();
print(inputList);
Console.WriteLine("Output:");
var outputList = FillHoles(inputList);
print(outputList);
Console.WriteLine("Done");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/314649.html
上一篇:我如何分組此資料表上的一列
下一篇:我想使用LINQ獲取最頻繁的值
