2011-12-13 70 views
2

我看過這個Flatten LINQ collection object with nested object collections,但它不完全適合我。拼合LINQ系列

我知道在這篇文章中有很多代碼,但它主要是數據給你我想要開發的東西的想法。

如果你看看下面的類,我試圖想出一種方法來平滑搜索文件的結果。

所以我需要有一個扁平的記錄,它看起來像落得(該管道是那裏顯示的字段delimination只)

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

不通過每個循環我如何能做到這一點的任何想法聯繫人和客戶?

我在我的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; } 
} 

而這作爲數據只是爲了測試。

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 }); 
    } 
} 

回答

3

這將讓你包含您指定的順序屬性的IEnumerable<string>

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() 
    })); 

例子:http://ideone.com/Mvc7M