我一直在搜索Select和SelectMany之间的区别,但我还没有找到合适的答案。我需要学习使用LINQ to SQL时的差异,但我所找到的都是标准数组示例。
有人能提供一个LINQ到SQL的例子吗?
我一直在搜索Select和SelectMany之间的区别,但我还没有找到合适的答案。我需要学习使用LINQ to SQL时的差异,但我所找到的都是标准数组示例。
有人能提供一个LINQ到SQL的例子吗?
当前回答
我认为这是理解的最好方式。
var query =
Enumerable
.Range(1, 10)
.SelectMany(ints => Enumerable.Range(1, 10), (a, b) => $"{a} * {b} = {a * b}")
.ToArray();
Console.WriteLine(string.Join(Environment.NewLine, query));
Console.Read();
乘法表的例子。
其他回答
SelectMany()允许您以一种需要第二个Select()或循环的方式折叠多维序列。
更多细节请参见这篇博客文章。
假设你有一组国家
var countries = new[] { "France", "Italy" };
如果你对国家执行Select,你将得到数组中的每个元素IEnumerable<T>
IEnumerable<string> selectQuery = countries.Select(country => country);
在上面的代码中,国家表示指向数组中每个国家的字符串。现在迭代selectQuery以获得国家:
foreach(var country in selectQuery)
Console.WriteLine(country);
// output
//
// France
// Italy
如果你想打印每个国家的字符,你必须使用嵌套foreach
foreach (var country in selectQuery)
{
foreach (var charOfCountry in country)
{
Console.Write(charOfCountry + ", ");
}
}
// output
// F, r, a, n, c, e, I, t, a, l, y,
好的。现在尝试对国家执行SelectMany。这一次SelectMany获取每个国家作为字符串(和以前一样),因为字符串类型是一个字符的集合,SelectMany尝试将每个国家分为它的组成部分(字符),然后返回一个字符的集合IEnumerable<T>
IEnumerable<char> selectManyQuery = countries.SelectMany(country => country);
在上面的代码中,country表示一个字符串,该字符串引用数组中的每个国家,但返回值是每个国家的字符
实际上SelectMany喜欢在集合中获取两层,并将第二层平铺为IEnumerable<T>
现在遍历selectManyQuery以获得每个国家的字符:
foreach(var charOfCountry in selectManyQuery)
Console.Write(charOfCountry + ", ");
// output
// F, r, a, n, c, e, I, t, a, l, y,
再举一个如何使用SelectMany + Select来累积子数组对象数据的例子。
假设我们有一些带着手机的用户:
class Phone {
public string BasePart = "555-xxx-xxx";
}
class User {
public string Name = "Xxxxx";
public List<Phone> Phones;
}
现在我们需要选择所有用户的所有手机的basepart:
var usersArray = new List<User>(); // array of arrays
List<string> allBaseParts = usersArray.SelectMany(ua => ua.Phones).Select(p => p.BasePart).ToList();
SelectMany扁平查询,返回列表的列表。例如
public class PhoneNumber
{
public string Number { get; set; }
}
public class Person
{
public IEnumerable<PhoneNumber> PhoneNumbers { get; set; }
public string Name { get; set; }
}
IEnumerable<Person> people = new List<Person>();
// Select gets a list of lists of phone numbers
IEnumerable<IEnumerable<PhoneNumber>> phoneLists = people.Select(p => p.PhoneNumbers);
// SelectMany flattens it to just a list of phone numbers.
IEnumerable<PhoneNumber> phoneNumbers = people.SelectMany(p => p.PhoneNumbers);
// And to include data from the parent in the result:
// pass an expression to the second parameter (resultSelector) in the overload:
var directory = people
.SelectMany(p => p.PhoneNumbers,
(parent, child) => new { parent.Name, child.Number });
Live Demo。net Fiddle
我认为这是理解的最好方式。
var query =
Enumerable
.Range(1, 10)
.SelectMany(ints => Enumerable.Range(1, 10), (a, b) => $"{a} * {b} = {a * b}")
.ToArray();
Console.WriteLine(string.Join(Environment.NewLine, query));
Console.Read();
乘法表的例子。