griegs
griegs

Reputation: 22760

Flatten LINQ Collection

I have had a look at this Flatten LINQ collection object with nested object collections but it doesn't quite do it for me.

I know there is a lot of code in this post but it's mostly just data to give you the idea of what I'm looking at developing.

if you look at the classes below, I am trying to come up with a way to flatten the result of a search against the file.

So i need to end up with a single flattened record which looks like (the pipes are there to show delimination of a field only)

fileId | FileContact1FirstName | FileContact1LastName | FileContact2FirstName etc | FileClient1FirstName  | FileClient1LastName | FileClient1IsNominee | FileClient1IsPrimary | FileClient2FirstName etc....

Any idea on how I can do this without looping through each Contact and Client?

I have these classes of sorts in my edmx;

class File
{
    public int fileId { get; set; }
    public List<FileContact> fileContacts { get; set; }
    public List<FileClient> fileClients { get; set; }
}

class FileContact
{
    public Contact contact { get; set; }
}

class FileClient
{
    public Contact contact { get; set; }
    public bool IsNominee { get; set; }
    public bool IsPrimary { get; set; }
}

class Contact
{
    public int id { get; set; }
    public string firstName { get; set; }
    public string lastName { get; set; }
}

And this this as the data simply for testing.

    static void FillData()
    {
        thisFile = new File { fileId = 1, fileContacts = new List<FileContact>(), fileClients = new List<FileClient>() };

        thisFile.fileContacts.Add(new FileContact { contact = new Contact { id = 1, firstName = "Andrew", lastName = "Albino" } });
        thisFile.fileContacts.Add(new FileContact { contact = new Contact { id = 1, firstName = "Bob", lastName = "Bush" } });
        thisFile.fileContacts.Add(new FileContact { contact = new Contact { id = 1, firstName = "Cathy", lastName = "Conti" } });
        thisFile.fileContacts.Add(new FileContact { contact = new Contact { id = 1, firstName = "Drew", lastName = "Dram" } });
        thisFile.fileContacts.Add(new FileContact { contact = new Contact { id = 1, firstName = "Edward", lastName = "Eliston" } });
        thisFile.fileContacts.Add(new FileContact { contact = new Contact { id = 1, firstName = "Frank", lastName = "Fashion" } });
        thisFile.fileContacts.Add(new FileContact { contact = new Contact { id = 1, firstName = "Graham", lastName = "Grape" } });

        thisFile.fileClients.Add(new FileClient { contact = new Contact { id = 1, firstName = "Harry", lastName = "Who didn't" }, IsNominee = true, IsPrimary = false });
        thisFile.fileClients.Add(new FileClient { contact = new Contact { id = 1, firstName = "Indigo", lastName = "Ignacio" }, IsNominee = false, IsPrimary = false });
        thisFile.fileClients.Add(new FileClient { contact = new Contact { id = 1, firstName = "Julie", lastName = "Juniper" }, IsNominee = false, IsPrimary = false });
        thisFile.fileClients.Add(new FileClient { contact = new Contact { id = 1, firstName = "Kelly", lastName = "Keilor" }, IsNominee = false, IsPrimary = false });
        thisFile.fileClients.Add(new FileClient { contact = new Contact { id = 1, firstName = "Liam", lastName = "Loser" }, IsNominee = false, IsPrimary = true });
    }
}

Upvotes: 2

Views: 796

Answers (2)

Andrew Whitaker
Andrew Whitaker

Reputation: 126052

This will get you an IEnumerable<string> that contains the properties in the order you specified:

var flattened = new string[] { thisFile.fileId.ToString() }
.Concat(
    thisFile.fileContacts
    .SelectMany(fc => new string[] 
    { 
        fc.contact.firstName, 
        fc.contact.lastName 
    }))
.Concat(
    thisFile.fileClients
    .SelectMany(fc => new string[] 
    { 
        fc.contact.firstName, 
        fc.contact.lastName, 
        fc.IsNominee.ToString(), 
        fc.IsPrimary.ToString() 
    }));

Example: http://ideone.com/Mvc7M

Upvotes: 3

Rohan West
Rohan West

Reputation: 9298

Have a look at SelectMany.

Upvotes: 1

Related Questions