4

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

Mike
  • 850
  • 10
  • 33
hiFI
  • 1,887
  • 3
  • 28
  • 57
  • 2
    Something like RequestApprovers= g.Select(c => new RequestApprover{ApproverUserId = c.ApproverUserId, ApproverUserName = c.ApproverUserName).ToList() – Janus Pienaar Jun 19 '18 at 07:50

1 Answers1

6

To achieve this goal you can you Select method with creation required object in it. I assume this should work for you:

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() 
            RequestApprovers = g.Select(c => new RequestApprover(){ApproverUserName = c.ApproverUserName, ApproverUserId = c.ApproverUserId}).ToList()
        }).ToList();
Alex Riabov
  • 8,655
  • 5
  • 47
  • 48
  • 2
    Excellent! Please note there is a small correction to be made: `RequestWithApprover.RequestApprovers` accepts `List` and not `List` :) – hiFI Jun 19 '18 at 08:03