gpt4 book ai didi

c# - 复杂的 Linq 分组

转载 作者:可可西里 更新时间:2023-11-01 07:58:46 25 4
gpt4 key购买 nike

我是 Stack Overflow 的新手,但尝试提供尽可能多的信息

我有以下类结构

public class ItemEntity
{
public int ItemId { get; set; }
public int GroupId { get; set; }
public string GroupName { get; set; }
public DateTime ItemDate { get; set; }
public string Field1 { get; set; }
public string Filed2 { get; set; }
public string Field3 { get; set; }
public string Field4 { get; set; }
public int Duration { get; set; }
}

public class MasterEntity
{
public ItemEntity Item { get; set; }
public List<int> ItemList { get; set; }
public List<int> GroupList { get; set; }
}

我正在尝试将 ItemEntity 列表分组到 MasterEntity 中。分组字段为 Field1、Field2 和 Field3。

到目前为止,我已经完成了分组,如下所示

var items = new List<ItemEntity>
{
new ItemEntity
{
ItemId = 100,
GroupId = 1,
GroupName= "Group 1",
ItemDate = new DateTime(2018,10,17),
Duration = 7,
Field1 = "Item Name 1",
Filed2 = "aaa",
Field3= "bbb",
Field4= "abc"
},
new ItemEntity
{
ItemId = 150,
GroupId = 2,
GroupName= "Group 2",
ItemDate = new DateTime(2018,10,17),
Duration = 5,
Field1 = "Item Name 1",
Filed2 = "aaa",
Field3= "bbb",
Field4= "efg"
},
new ItemEntity
{
ItemId = 250,
GroupId = 3,
GroupName= "Group 3",
ItemDate = new DateTime(2018,10,15),
Duration = 7,
Field1 = "Item Name 1",
Filed2 = "aaa",
Field3= "bbb",
Field4= "xyz"
}
};


var group = items.GroupBy(g => new
{
g.Field1,
g.Filed2,
g.Field3
}).Select(s => new MasterEntity
{
Item = new ItemEntity
{
Field1 = s.Key.Field1,
Filed2 = s.Key.Filed2,
Field3 = s.Key.Field3
},
ItemList = s.Select(g => g.ItemId).ToList(),
GroupList = s.Select(g => g.GroupId).ToList()
}).ToList();

在这个组中,我想根据实际的 ItemDate 和 Duration 进一步拆分它,如下所示

Expected Output

基本上,在这种情况下,我想将这个组分成三个。

因为只有 Group3 的日期是 15 号到 17 号,所以这将是一组。从第 17 到第 22 组 Group1,Group2 和 Group3 相同。所以那将成为另一组。最后只有 Group1 有 22 到 24 所以它成为另一个组

最终分组的数据要像

G1
{
ItemEntity :{
ItemDate : 15/10/2018,
Duration : 2,
Field1 : "Item Name 1",
Filed2 : "aaa",
Field3 : "bbb",
},
ItemList: {250},
GroupList:{3}
}

,
G2
{
ItemEntity :{
ItemDate : 17/10/2018,
Duration : 5,
Field1 : "Item Name 1",
Filed2 : "aaa",
Field3 : "bbb",
},
ItemList: {100,150,250},
GroupList:{1,2,3}
}
,
G3
{
ItemEntity :{
ItemDate : 22/10/2018,
Duration : 2,
Field1 : "Item Name 1",
Filed2 : "aaa",
Field3 : "bbb",
},
ItemList: {100},
GroupList:{1}
}

最佳答案

这非常具有挑战性。我使用了一些方便的扩展方法,我已经不得不让它变得更容易,并创建了一个默认使用 SetEqualHashSet 子类(.Net 确实需要一些内置的成员相等集合类 -中)。

首先是类HashSetEq,它在成员匹配时实现相等:

public class HashSetEq<T> : HashSet<T>, IEquatable<HashSetEq<T>> {
private static readonly IEqualityComparer<HashSet<T>> SetEq = HashSet<T>.CreateSetComparer();

public override int GetHashCode() => SetEq.GetHashCode(this);
public override bool Equals(object obj) => obj != null && (obj is HashSetEq<T> hs) && this.Equals(hs);
public bool Equals(HashSetEq<T> other) => SetEq.Equals(this, other);

public HashSetEq(IEnumerable<T> src) : base(src) {
}
}

现在,IEnumerable 的一些扩展。一个扩展将 IEnumerable 转换为 HashSetEq 以便于创建键集合。另一个扩展是 GroupBy 的变体,它基于实现 APL 扫描运算符的成对版本的扩展 ScanPair,在谓词为真时进行分组。

public static class IEnumerableExt {
public static HashSetEq<T> ToHashSetEq<T>(this IEnumerable<T> src) => new HashSetEq<T>(src);


// TKey combineFn((TKey Key, T Value) PrevKeyItem, T curItem):
// PrevKeyItem.Key = Previous Key
// PrevKeyItem.Value = Previous Item
// curItem = Current Item
// returns new Key
public static IEnumerable<(TKey Key, T Value)> ScanPair<T, TKey>(this IEnumerable<T> src, TKey seedKey, Func<(TKey Key, T Value), T, TKey> combineFn) {
using (var srce = src.GetEnumerator()) {
if (srce.MoveNext()) {
var prevkv = (seedKey, srce.Current);

while (srce.MoveNext()) {
yield return prevkv;
prevkv = (combineFn(prevkv, srce.Current), srce.Current);
}
yield return prevkv;
}
}
}

public static IEnumerable<IGrouping<int, T>> GroupByWhile<T>(this IEnumerable<T> src, Func<T, T, bool> testFn) =>
src.ScanPair(1, (kvp, cur) => testFn(kvp.Value, cur) ? kvp.Key : kvp.Key + 1)
.GroupBy(kvp => kvp.Key, kvp => kvp.Value);
}

为了对日期跨度进行分组,我基于 GroupByWhile 内联扩展了我的 GroupBySequential,这样我就可以按连续的日期运行和匹配的 GroupId 集进行分组 GroupBySequential 依赖于一个整数序列,所以我需要一个基准日期来计算日期序列号,所以我在所有项目中使用最早的日期:

var baseDate = items.Min(i => i.ItemDate);

现在我可以计算答案了。

对于每组项目,我根据 Duration 将每个项目扩展到它涵盖的所有日期,并将每个日期与原始项目相关联:

var group = items.GroupBy(g => new {
g.Field1,
g.Filed2,
g.Field3
})
.Select(g => g.SelectMany(i => Enumerable.Range(0, i.Duration).Select(d => new { ItemDate = i.ItemDate.AddDays(d), i }))

现在我有了所有单独的日期+项目,我可以为每个日期对它们进行分组。

              .GroupBy(di => di.ItemDate)

然后将每个日期+项目按日期和该日期的组集分组,并按日期排序。

              .GroupBy(dig => new { ItemDate = dig.Key, Groups = dig.Select(di => di.i.GroupId).ToHashSetEq() })
.OrderBy(ig => ig.Key.ItemDate)

通过按日期排序,我可以将具有相同 Groups 的连续日期分组在一起(使用 baseDate 中的天数)。

              .GroupByWhile((prevg, curg) => (int)(prevg.Key.ItemDate - baseDate).TotalDays + 1 == (int)(curg.Key.ItemDate - baseDate).TotalDays && prevg.Key.Groups.Equals(curg.Key.Groups))

最后,我可以将每个顺序日期组的信息提取到一个 MasterEntity 中,并使整个答案成为一个 List

              .Select(igg => new MasterEntity {
Item = new ItemEntity {
ItemDate = igg.First().Key.ItemDate,
Duration = igg.Count(),
Field1 = g.Key.Field1,
Filed2 = g.Key.Filed2,
Field3 = g.Key.Field3
},
ItemList = igg.First().First().Select(di => di.i.ItemId).ToList(),
GroupList = igg.First().Key.Groups.ToList()
})
)
.ToList();

关于c# - 复杂的 Linq 分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52889371/

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