I have an entity like this:
class MyEntity {
public int Id{ get; set; }
public string Type { get; set; }
public EventData Data { get; set; }
// other stuff...
}
where EventData is something like:
class EventData {
public string GroupId { get; set; }
public DateTime CreationDate { get; set; }
public DateTime? TerminationDate { get; set; }
// other stuff...
}
when "GroupId" is null or empty the entity is not part of a group. Entities with the same "GroupId" are part of the same group.
What i need to achieve is a list of groups in which the key is a complex type (anonymous or not... i don't know) and the corresponding value is the list of entities.
Each group key must be a complex type because i will use it to sort the groups according to one of its properties.
For example the key will be:
class MyKey {
public bool IsGroup { get; set; }
public string GroupId { get; set; }
public DateTime CreationDate { get; set; }
}
Indeed, if "IsGroup" is false (and GroupId is null), the corresponding value will be a list containing only one entity.
The object should be something like:
List<IGrouping<Mykey, List<MyEntity>> ...
Thank you for your help. I hope it's clear enough :)
INPUT EXAMPLE
List<MyEntity> entities = new List<MyEntity>()
{
new MyEntity {
Id = 1,
Data = new EventData {
GroupId = null
}
},
new MyEntity {
Id = 2,
Data = new EventData {
GroupId = null
}
},
new MyEntity {
Id = 3,
Data = new EventData {
GroupId = "1"
}
},
new MyEntity {
Id = 4,
Data = new EventData {
GroupId = "1"
}
}
}
Starting from this simplyfied input example, the output should be a list with 3 objects:
- Key with IsGroup=false and GroupId=null. The value is a list containing only the entity with id=1.
- Key with IsGroup=false and GroupId=null. The value is a list containing only the entity with id=2.
- Key with IsGroup=true and GroupId="1". The value is a list containing the entities with id=3 and id=4.