Overview
When I have a class as
public class RequestEntry
{
public int RequestId
{
get;
set;
}
public string ApproverUserId
{
get;
set;
}
public string ApproverUserName
{
get;
set;
}
public DateTime RequestedDate
{
get;
set;
}
}
and it includes in a List as:
List<RequestEntry> RequestEntries = new List<RequestEntry>()
{
new RequestEntry{RequestId = 1, RequestedDate = new DateTime(2018,06,01), ApproverUserId = "STEVES", ApproverUserName = "Steve Smith"},
new RequestEntry{RequestId = 1, RequestedDate = new DateTime(2018,06,01), ApproverUserId = "GRAHAMS", ApproverUserName = "Graham Smith" },
new RequestEntry{RequestId = 2, RequestedDate = new DateTime(2018,06,02), ApproverUserId = "STEVES", ApproverUserName = "Steve Smith"},
new RequestEntry{RequestId = 3, RequestedDate = new DateTime(2018,06,03), ApproverUserId = "ROBINS", ApproverUserName = "Robin Smith"},
new RequestEntry{RequestId = 3, RequestedDate = new DateTime(2018,06,03), ApproverUserId = "CHRISS", ApproverUserName = "Chris Smith"},
new RequestEntry{RequestId = 3, RequestedDate = new DateTime(2018,06,03), ApproverUserId = "LIAMS", ApproverUserName = "Liam Smith"}
};
Objective
My Expected Result is to be in such way:
public class RequestWithApprover
{
public int RequestId
{
get;
set;
}
public DateTime RequestedDate
{
get;
set;
}
public List<string> ApproverUserIds
{
get;
set;
}
public List<RequestApprover> RequestApprovers
{
get;
set;
}
}
Where RequestApprover
is defined as
public class RequestApprover
{
public string ApproverUserId
{
get;
set;
}
public string ApproverUserName
{
get;
set;
}
}
Workings
After following some earlier scenarios in Stackoverflow, I was able to write to the extent of getting a List as defined in
var results = (
from r in RequestEntries
group r by new
{
r.RequestId, r.RequestedDate
}
into g
select new RequestWithApprover()
{
RequestId = g.Key.RequestId,
RequestedDate = g.Key.RequestedDate,
ApproverUserIds = g.Select(c => c.ApproverUserId).ToList()
//----> Here I am able to get a List<string> but need to be in List<RequestWithApprover>
}).ToList();
How Can I get the result to List<RequestWithApprover>
?
My DotNetFiddle