展平LINQ系列

时间:2011-12-13 03:26:14

标签: c# linq

我已经看过这个Flatten LINQ collection object with nested object collections,但对我来说并不是这样。

我知道这篇文章中有很多代码,但它主要是为了让您了解我正在开发的内容。

如果你看一下下面的课程,我试图想出一种方法来压缩搜索文件的结果。

所以我需要最终得到一个看似扁平的记录(管道只显示一个字段的消除)

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

查看SelectMany