- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我是 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 进一步拆分它,如下所示
基本上,在这种情况下,我想将这个组分成三个。
因为只有 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}
}
最佳答案
这非常具有挑战性。我使用了一些方便的扩展方法,我已经不得不让它变得更容易,并创建了一个默认使用 SetEqual
的 HashSet
子类(.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/
您好,我正在处理 BIRT 报告。我有一个查询,我必须对父级的重复数据进行分组,但子级也不能分组! 在我的查询中: item 是父项,item_ledger_entry 是子项。我有来自 item.N
我正在使用 GA API。 这是针对 MCF 目标报告(底部)的标准目标完成指标表(顶部) 看一下这个: 总数加起来 (12,238),但看看按 channel 分组的分割有多么不同!我以为这些会很接
我正在开发一个流量计数器,我想获得 IP 和重复计数,但是如何? 就像是 :select ip, count(ip) from Redirect 返回 : null total ip count 重定
我尝试编写一个正则表达式来匹配条件表达式,例如: a!=2 1+2=2+a 我尝试提取运算符。我当前的正则表达式是“.+([!=<>]+).+” 但问题是匹配器总是尝试匹配组中可能的最短字符串
在 MS Transact SQL 中,假设我有一个这样的表(订单): Order Date Order Total Customer # 09/30/2008 8
我想按 m.ID 分组,并对每个 m.id 求和 (pm.amount_construction* prod.anzahl) 实际上我有以下结果: Meterial_id | amount_const
我想根据多列中的值对值进行分组。这是一个例子: 我想得到输出: {{-30,-50,20},{-20,30,60},{-30,NULL or other value, 20}} 我设法到达: SELE
我正在尝试找出运行此查询的最佳方式。我基本上需要返回在我们的系统中只下了一个订单的客户的“登录”字段列表(登录字段基本上是客户 ID/ key )。 我们系统的一些背景...... 客户在同一日期下的
给定以下mysql结果集: id code name importance '1234', 'ID-CS-B', 'Chocolate Sauce'
大家好,我的数据框中有以下列: LC_REF 1 DT 16 2C 2 DT 16 2C 3 DT 16 2C 1 DT 16 3C 6 DT 16 3C 3
我有这样的 mongoDB 集合 { "_id" : "EkKTRrpH4FY9AuRLj", "stage" : 10, }, { "_id" : "EkKTRrpH4FY9
假设我有一组数据对,其中 index 0 是值,index 1 是类型: input = [ ('11013331', 'KAT'), ('9085267',
java中用stream进行去重,排序,分组 一、distinct 1. 八大基本数据类型 List collect = ListUtil.of(1, 2, 3, 1, 2).stream().fil
基本上,我从 TABLE_A 中的这个开始 France - 100 France - 200 France - 300 Mexico - 50 Mexico - 50 Mexico - 56 Pol
我希望这个正则表达式 ([A-Z]+)$ 将选择此示例中的最后一次出现: AB.012.00.022ABC-1 AB.013.00.022AB-1 AB.014.00.022ABAB-1 但我没有匹配
我创建了一个数据透视表,但数据没有组合在一起。 任何人都可以帮助我获得所需的格式吗? 我为获取数据透视表而编写的查询: DECLARE @cols AS NVARCHAR(MAX), -- f
我想按时间段(月,周,日,小时,...)选择计数和分组。例如,我想选择行数并将它们按 24 小时分组。 我的表创建如下。日期是时间戳。 CREATE TABLE MSG ( MSG_ID dec
在 SQL Server 2005 中,我有一个包含如下数据的表: WTN------------Date 555-111-1212 2009-01-01 555-111-1212 2009-
题 假设我有 k 个标量列,如果它们沿着每列彼此在一定距离内,我想对它们进行分组。 假设简单 k 是 2 并且它们是我唯一的列。 pd.DataFrame(list(zip(sorted(choice
问题 在以下数据框中 df : import random import pandas as pd random.seed(999) sz = 50 qty = {'one': 1, 'two': 2
我是一名优秀的程序员,十分优秀!