gpt4 book ai didi

c# - 展平 LINQ 集合

转载 作者:行者123 更新时间:2023-11-30 21:10:28 29 4
gpt4 key购买 nike

我看过这个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 });
}
}

最佳答案

这会给你一个 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

关于c# - 展平 LINQ 集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8483965/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com