需要有关 C# LINQ 列表聚合表达式的建议
Need advice for C# LINQ List aggregation expression
假设我有价格类型列表:
[
{
"Id": 57571,
"Price": 1745.0,
"DateAdded": "2018-12-01T00:00:00"
},
{
"Id": 67537,
"Price": 1695.0,
"DateAdded": "2018-09-24T00:00:00"
},
{
"Id": 80042,
"Price": 1645.0,
"DateAdded": "2019-03-24T00:00:00"
},
{
"Id": 155866,
"Price": 1545.0,
"DateAdded": "2019-04-24T00:00:00"
},
{
"Id": 163643,
"Price": 1545.0,
"DateAdded": "2019-04-26T00:00:00"
},
{
"Id": 171379,
"Price": 1545.0,
"DateAdded": "2019-04-27T00:00:00"
},
{
"Id": 178990,
"Price": 1545.0,
"DateAdded": "2019-04-28T00:00:00"
}
]
我需要删除所有具有相同价格的项目,但前提是该项目是列表元素的兄弟项。有没有用LINQ实现的?
我的预期输出是
[
{
"Id": 57571,
"Price": 1745.0,
"DateAdded": "2018-12-01T00:00:00"
},
{
"Id": 67537,
"Price": 1695.0,
"DateAdded": "2018-09-24T00:00:00"
},
{
"Id": 80042,
"Price": 1645.0,
"DateAdded": "2019-03-24T00:00:00"
},
{
"Id": 155866,
"Price": 1545.0,
"DateAdded": "2019-04-24T00:00:00"
}
]
我完全不知道如何才能做到这一点。我很感激任何建议。
您可以简单地使用迭代器这样做:
using System;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
var input = new[] { 1, 2, 2, 3, 4, 4, 3, 2, 2, 1 };
var output = GetUniqueAdjacent(input);
foreach (var o in output)
{
Console.WriteLine(o);
}
}
public static IEnumerable<int> GetUniqueAdjacent(IEnumerable<int> input)
{
bool first = true;
int previous = -1;
foreach (var i in input)
{
if (first)
{
previous = i;
yield return i;
first = false;
continue;
}
if (i == previous)
{
previous = i;
continue;
}
previous = i;
yield return i;
}
}
}
这会输出 1, 2, 3, 4, 3, 2, 1
,仅删除相邻的重复项。
通过使用 Microsoft 的 C# 文档中的 ChunkBy
extension 方法,您可以
myData.ChunkBy(x => x.Price).Select(g => g.First())
...或者使用 MoreLinq's GroupAdjacent
你可以
myData.GroupAdjacent(x => x.Price).Select(g => g.First())
这可以通过常规 Where
调用来完成。
int? previousPrice = null;
var output = myData.Where(d => {
var result = !previousPrice.HasValue || previousPrice.Value != d.Price;
previousPrice = d.Price;
return result;
}).ToList();
假设我有价格类型列表:
[
{
"Id": 57571,
"Price": 1745.0,
"DateAdded": "2018-12-01T00:00:00"
},
{
"Id": 67537,
"Price": 1695.0,
"DateAdded": "2018-09-24T00:00:00"
},
{
"Id": 80042,
"Price": 1645.0,
"DateAdded": "2019-03-24T00:00:00"
},
{
"Id": 155866,
"Price": 1545.0,
"DateAdded": "2019-04-24T00:00:00"
},
{
"Id": 163643,
"Price": 1545.0,
"DateAdded": "2019-04-26T00:00:00"
},
{
"Id": 171379,
"Price": 1545.0,
"DateAdded": "2019-04-27T00:00:00"
},
{
"Id": 178990,
"Price": 1545.0,
"DateAdded": "2019-04-28T00:00:00"
}
]
我需要删除所有具有相同价格的项目,但前提是该项目是列表元素的兄弟项。有没有用LINQ实现的?
我的预期输出是
[
{
"Id": 57571,
"Price": 1745.0,
"DateAdded": "2018-12-01T00:00:00"
},
{
"Id": 67537,
"Price": 1695.0,
"DateAdded": "2018-09-24T00:00:00"
},
{
"Id": 80042,
"Price": 1645.0,
"DateAdded": "2019-03-24T00:00:00"
},
{
"Id": 155866,
"Price": 1545.0,
"DateAdded": "2019-04-24T00:00:00"
}
]
我完全不知道如何才能做到这一点。我很感激任何建议。
您可以简单地使用迭代器这样做:
using System;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
var input = new[] { 1, 2, 2, 3, 4, 4, 3, 2, 2, 1 };
var output = GetUniqueAdjacent(input);
foreach (var o in output)
{
Console.WriteLine(o);
}
}
public static IEnumerable<int> GetUniqueAdjacent(IEnumerable<int> input)
{
bool first = true;
int previous = -1;
foreach (var i in input)
{
if (first)
{
previous = i;
yield return i;
first = false;
continue;
}
if (i == previous)
{
previous = i;
continue;
}
previous = i;
yield return i;
}
}
}
这会输出 1, 2, 3, 4, 3, 2, 1
,仅删除相邻的重复项。
通过使用 Microsoft 的 C# 文档中的 ChunkBy
extension 方法,您可以
myData.ChunkBy(x => x.Price).Select(g => g.First())
...或者使用 MoreLinq's GroupAdjacent
你可以
myData.GroupAdjacent(x => x.Price).Select(g => g.First())
这可以通过常规 Where
调用来完成。
int? previousPrice = null;
var output = myData.Where(d => {
var result = !previousPrice.HasValue || previousPrice.Value != d.Price;
previousPrice = d.Price;
return result;
}).ToList();