如何组合组中的多个列表?
如果我有一个查询组,并且想组合多个列表,如何将列表组合到一个列表中。为具有属性类列表的对象会话说。理想情况下,也从列表中删除所有重复值。
例子
data.GroupBy(x => x.SchoolId).Select( a => new SchoolObject {
// this query doesnt work but I r.ListIWantToGroup is a list, that I would like to combine with other lists in group by
// and if possible also eliminate duplicates from lists by one of objects properties, note list is filled with complex object and not primitive type of so I dont know if thats possible, item I would want to prevent duplicates on would be r.ListIWantToGroup[index].Value
ExampleList = a.Select( r => r.ListIWantToGroup)
}).ToList();
//Example Model for List
public class ListIWantToGroup {
public string PropertyName {get; set; }
public string Value {get; set; }
}
// Example Model for object
public class SchoolObject{
public string Name {get; set; }
public int Id{get; set; }
public List<itemIwanttogroup> ExampleList {get; set;)
}
If I have a group by query and I want to combine multiple lists how can I combine the lists into one. Say for a object session with properties class list. Ideally remove any duplicate values from list too.
Example
data.GroupBy(x => x.SchoolId).Select( a => new SchoolObject {
// this query doesnt work but I r.ListIWantToGroup is a list, that I would like to combine with other lists in group by
// and if possible also eliminate duplicates from lists by one of objects properties, note list is filled with complex object and not primitive type of so I dont know if thats possible, item I would want to prevent duplicates on would be r.ListIWantToGroup[index].Value
ExampleList = a.Select( r => r.ListIWantToGroup)
}).ToList();
//Example Model for List
public class ListIWantToGroup {
public string PropertyName {get; set; }
public string Value {get; set; }
}
// Example Model for object
public class SchoolObject{
public string Name {get; set; }
public int Id{get; set; }
public List<itemIwanttogroup> ExampleList {get; set;)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
目前尚不清楚您的目标是什么,尽管听起来您正在尝试根据对象的单个属性删除重复项,
让我们谈论Groupby;希望它可以帮助您弄清楚您需要
在单个输入列表上运行的groupby需要做什么,可以说一个1维数组,并将尺寸的数量增加1。分组1D数组的输出概念上是一个2D数组
您有一些学校:
您将其按SchoolID进行分组。输出是一堆Igroupings,每个Igroupings在概念上都是原始对象的数组/列表。每个唯一键都有一个igroup。每个IGrouping都包含上面样本中共享该密钥的所有对象
,两所学校具有ID 1,一所学校具有ID 2。分组的输出将为两个Igroupings。其中一个有1个钥匙,并包含两个学校对象。另一个具有2个键,包含一个学校对象,
在JSON中表示并不容易,因为JSON没有一个具有关键属性的对象的概念,也是对象的列表,但可以想象“自我“上面是通过对特定分组的一名阵列进行列举而产生的学校列表
:
当您针对这款新的锯齿状2D事物运行时,您正在选择代表内部列表的igroup对象。为此,我通常将输入与选择为
g
,因为它是一个分组(igrouping)。小组的成员共享密钥。让我们每张ID中的第一所学校:
请记住,
g
实际上是学校的列表,因此g.first()
这是一所学校。从每个内部列表中选择第一个的输出是您每个ID获得一所学校。学校A2丢失了:g.first()
的输出是一所学校,因此SELECT的输出是ienumerable&lt; school&gt;
- 所有分组都消失了,学校A被保留,学校B也被保留,而学校A2也消失了,如果您完成了
g.last()
,Schhol A2将被保留,而学校A将失去Groupby的相反操作,从概念上讲selectany;您将其在2D中给它一个内部列表,然后将所有内部列表组合到一个列表中,从2D到1D。这是在评论中提到的,但我认为这不会是您想要的,因为它会让您回到自己的位置/撤消Groupby。如果您正在做诸如“学校列表,x组,d下订购”,“取得Z级记录”,将2D返回到1D列表之类的事情,则可以使用它。
It's not entirely clear what your aim is, though it sounds like you're trying to remove duplicates based on a single property of an object
Let's talk about GroupBy; hopefully it help further you towards figuring out what you need to do
GroupBy operates on a single input list, lets say a 1 dimensional array, and it increases the number of dimensions by 1. The output of grouping a 1D array is conceptually a 2D array
You have some schools:
You group this by SchoolId. The output is bunch of IGroupings, each of which is conceptually an array/list of the original objects. There is one IGrouping for each unique key. Each IGrouping contains all the objects that share that key
In the sample above, two schools have ID 1 and one school has ID 2. The output of the grouping will be two IGroupings. One of them has a Key of 1 and contains two school objects. The other has a Key of 2 and contains one school object
It's not so easy to represent in json because json doesn't have a concept of an object that has a Key property and is also a list of objects, but imagine that the "self" above is the list of schools that would arise from enumerating over a particular grouping
Your 1D array has gone jagged 2D:
When you run a Select against this new jagged 2D thing you're selecting IGrouping objects that represent the inner lists. To this end I typically alias the input to the select as
g
because it's a grouping (IGrouping). The members of the group share a key.Let's pull just the first school per id:
Remember that
g
is effectively a list of schools sog.First()
here is a single school. The output of selecting the first from each inner list is that you get one school per Id. School A2 is lost:The output of
g.First()
is a School, so the output of Select is anIEnumerable<School>
- all the groupings are gone, School A is kept, as is School B, and School A2 is goneHad you done
g.Last()
, Schhol A2 would be kept and School A would be lostThe opposite operation of a GroupBy is conceptually a SelectMany; you give it an inner list in a 2D and it combines all the inner lists into one list, going from 2D to 1D again. It was mentioned in the comments but I don't think it'll be what you want because it puts you back to where you were/undoes the GroupBy. You might use it if you were doing something like "list of schools, group by x, order each grouping by y descending, take top z records, expand 2D back to 1D list"