C# 展平LINQ集合
我已经看过了,但对我来说不太合适 我知道这篇文章中有很多代码,但大部分只是数据,让您了解我正在开发的内容 如果你看一下下面的类,我试图找到一种方法来平展对该文件的搜索结果 因此,我需要以一个扁平的记录结束,它看起来像(管道只是为了显示字段的删除) 你知道我如何做到这一点而不必通过每个联系人和客户进行循环吗 在我的edmx中有这样的类C# 展平LINQ集合,c#,linq,C#,Linq,我已经看过了,但对我来说不太合适 我知道这篇文章中有很多代码,但大部分只是数据,让您了解我正在开发的内容 如果你看一下下面的类,我试图找到一种方法来平展对该文件的搜索结果 因此,我需要以一个扁平的记录结束,它看起来像(管道只是为了显示字段的删除) 你知道我如何做到这一点而不必通过每个联系人和客户进行循环吗 在我的edmx中有这样的类 class File { public int fileId { get; set; } public List<FileContact>
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; }
}
类文件
{
public int fileId{get;set;}
公共列表文件联系人{get;set;}
公共列表文件客户端{get;set;}
}
类文件联系人
{
公共联系人{get;set;}
}
类文件客户端
{
公共联系人{get;set;}
公共bool是被提名者{get;set;}
公共bool IsPrimary{get;set;}
}
班级联系
{
公共int id{get;set;}
公共字符串名{get;set;}
公共字符串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 });
}
}
static void FillData()
{
thisFile=newfile{fileId=1,fileContacts=newlist(),fileClients=newlist()};
添加(新文件联系人{contact=newcontact{id=1,firstName=“Andrew”,lastName=“Albino”});
添加(新文件联系人{contact=newcontact{id=1,firstName=“Bob”,lastName=“Bush”});
添加(新文件联系人{contact=newcontact{id=1,firstName=“Cathy”,lastName=“Conti”});
添加(新文件联系人{contact=newcontact{id=1,firstName=“draw”,lastName=“Dram”});
添加(新文件联系人{contact=newcontact{id=1,firstName=“Edward”,lastName=“Eliston”});
添加(新文件联系人{contact=newcontact{id=1,firstName=“Frank”,lastName=“Fashion”});
添加(新文件联系人{contact=newcontact{id=1,firstName=“Graham”,lastName=“Grape”});
thisFile.fileClients.Add(newfileclient{contact=newcontact{id=1,firstName=“Harry”,lastName=“Who not”},isnamegate=true,IsPrimary=false});
添加(新文件客户端{contact=newcontact{id=1,firstName=“Indigo”,lastName=“Ignacio”},isnamegate=false,IsPrimary=false});
添加(新文件客户端{contact=newcontact{id=1,firstName=“Julie”,lastName=“Juniper”},isnamegate=false,IsPrimary=false});
添加(新文件客户端{contact=newcontact{id=1,firstName=“Kelly”,lastName=“Keilor”},isnamegate=false,IsPrimary=false});
thisFile.fileClients.Add(新文件客户端{contact=newcontact{id=1,firstName=“Liam”,lastName=“Loser”},isnamegate=false,IsPrimary=true});
}
}
看一看。这将获得一个IEnumerable
,它按指定的顺序包含属性:
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()
}));
示例:
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()
}));