gpt4 book ai didi

c# - 如何使用 LINQ 查找集合中的重叠(不是重复,而是查找重叠)

转载 作者:行者123 更新时间:2023-12-02 13:42:02 26 4
gpt4 key购买 nike

List<StoreDetailDto> items = new List<StoreDetailDto>();

items.Add(new StoreDetailDto { StoreNumber = 2, WarehouseNumber = 4201, IsResolved = false, StoreName = "StoreEx", WarehouseName = "WarehouseEx1"});
items.Add(new StoreDetailDto { StoreNumber = 3, WarehouseNumber = 4201, IsResolved = false, StoreName = "StoreEx", WarehouseName = "WarehouseEx1"});
items.Add(new StoreDetailDto { StoreNumber = 6, WarehouseNumber = 4201, IsResolved = false, StoreName = "StoreEx", WarehouseName = "WarehouseEx1"});
items.Add(new StoreDetailDto { StoreNumber = 7, WarehouseNumber = 4201, IsResolved = false, StoreName = "StoreEx", WarehouseName = "WarehouseEx1"});
items.Add(new StoreDetailDto { StoreNumber = 9, WarehouseNumber = 4201, IsResolved = false, StoreName = "StoreEx", WarehouseName = "WarehouseEx1"});

items.Add(new StoreDetailDto { StoreNumber = 6, WarehouseNumber = 4202, IsResolved = false, StoreName = "StoreEx", WarehouseName = "WarehouseEx1"});
items.Add(new StoreDetailDto { StoreNumber = 7, WarehouseNumber = 4201, IsResolved = false, StoreName = "StoreEx", WarehouseName = "WarehouseEx1"});
items.Add(new StoreDetailDto { StoreNumber = 9, WarehouseNumber = 4203, IsResolved = false, StoreName = "StoreEx", WarehouseName = "WarehouseEx1"});
items.Add(new StoreDetailDto { StoreNumber = 9, WarehouseNumber = 4207, IsResolved = false, StoreName = "StoreEx", WarehouseName = "WarehouseEx1"});

我上面有一个集合,其中商店 6、9 之间有重叠,但商店 7 不重叠,它是重复的。我发现同一家商店和不同的仓库有重叠。

为了实现我正在做的事情:

var overlapDupStores = items.GroupBy(
u => u.StoreNumber,
u => { return u; },
(key, g) => g.ToList())
.ToList()
.Where(cnt => cnt.Count() > 1);


foreach (var dpovl in overlapDupStores)
{
var stores = dpovl.Where(g => g.IsResolved != true).GroupBy(u => new { u.StoreNumber, u.WarehouseNumber }).ToList();

if (stores.Count() > 1)
{
response.OverlappingStores.AddRange(stores.SelectMany(gr => gr).ToList());
}
}

我首先找到重复的商店,它将在对象内部包含对象,其中包含计数 = 2 的商店重复项,然后按不同商店的商店编号和仓库编号进行分组,并且仓库计数仍为 2,但对于相同的商店和仓库这是重复的,它将是 count = 1,所以我发现 count > 1,这样我就可以找到重复项并检查 Isresolved,以便在解决后不要再次提取相同的存储。

下面是 fiddle :

https://dotnetfiddle.net/TknnkJ

最佳答案

尝试一下这个,使用 LINQ 的 GroupBy().Select().Where() 根据我假设您需要的条件(给定您显示的输出)对对象列表进行分组和过滤.

描述:

GroupBy() => Groups the objects using the StoreNumber property.
Select() => Selects the objects where the IsResolved property is false
Where() => Filters the objects that have more than 1 elements with different WarehouseNumber
SelectMany()=> Flattens the Grouping, to have all the matching objects in a sequential view

如果需要,可以添加

OrderBy() 以对 StoreNumber 上的列表进行排序 ThenBy() WarehouseNumber >.

Duplicates 过滤器可能不正确,具体取决于用例。可能有必要对其进行调整。

var Duplicates = 
items.GroupBy(store => store.StoreNumber)
.Select(grp => grp.Where(store => store.IsResolved == false))
.Where(stores => stores.Count() > 1 && stores.Select(w => w.WarehouseNumber).Distinct().Count() == 1)
.SelectMany(stores => stores)
.ToList();

var Overlapping =
items.GroupBy(store => store.StoreNumber)
.Select(grp => grp.Where(store => store.IsResolved == false))
.Where(store => store.Count() > 1 && store.Select(w => w.WarehouseNumber).Distinct().Count() > 1)
.SelectMany(stores => stores)
.ToList();

Overlapping.ForEach(ovr =>
Console.WriteLine($"{ovr.StoreNumber} " +
$"{ovr.WarehouseNumber} " +
$"{ovr.IsResolved} " +
$"{ovr.StoreName} " +
$"{ovr.WarehouseName}"));

重叠列表打印:

6 4201 False StoreEx WarehouseEx1
6 4202 False StoreEx WarehouseEx1
9 4201 False StoreEx WarehouseEx1
9 4203 False StoreEx WarehouseEx1
9 4207 False StoreEx WarehouseEx1

重复列表打印:

7 4201 False StoreEx WarehouseEx1
7 4201 False StoreEx WarehouseEx1

关于c# - 如何使用 LINQ 查找集合中的重叠(不是重复,而是查找重叠),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51825770/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com