如何将查询结果选择到多个列表中?例如,
class Person
{
public string FirstName {get;set;}
public string LastName {get;set;}
}
void test()
{
var query =
from i in Persons
select i;
// now i want to select two lists - list of first names, and list of last names
// approach 1 - run query twice?
List<string> f = query.Select( i=>i.FirstName).ToList();
List<string> l = query.Select( i=>i.LastName).ToList();
// approach 2 - turn it into a list first, and break it up
List<Person> p = query.ToList();
List<string> f = p.Select( i=>i.FirstName).ToList();
List<string> l = p.Select( i=>i.LastName).ToList();
}
方法 1 的问题是我需要运行两次查询。方法 2 的问题是我使用了两倍的内存。当数据集很大时,它可能会成为一个问题。