我一直在搜索Select和SelectMany之间的区别,但我还没有找到合适的答案。我需要学习使用LINQ to SQL时的差异,但我所找到的都是标准数组示例。
有人能提供一个LINQ到SQL的例子吗?
我一直在搜索Select和SelectMany之间的区别,但我还没有找到合适的答案。我需要学习使用LINQ to SQL时的差异,但我所找到的都是标准数组示例。
有人能提供一个LINQ到SQL的例子吗?
当前回答
select操作符用于从集合中选择值,SelectMany操作符用于从集合的集合(即嵌套集合)中选择值。
其他回答
Select是一个简单的从源元素到结果元素的一对一投影。选择- - - 当查询表达式中有多个from子句时使用Many:原始序列中的每个元素都用于生成一个新序列。
假设你有一组国家
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有几个重载。其中之一允许您在遍历层次结构时跟踪父节点和子节点之间的任何关系。
示例:假设您有以下结构:League -> Teams -> Player。
您可以很容易地返回一个平坦的播放器集合。但是你可能会失去任何关于玩家所在团队的信息。
幸运的是,有一个重载用于此目的:
var teamsAndTheirLeagues =
from helper in leagues.SelectMany
( l => l.Teams
, ( league, team ) => new { league, team } )
where helper.team.Players.Count > 2
&& helper.league.Teams.Count < 10
select new
{ LeagueID = helper.league.ID
, Team = helper.team
};
前面的例子来自Dan的IK博客。我强烈建议你看一看。
SelectMany()方法用于抚平序列,其中序列的每个元素都是独立的。
我的类用户是这样的
class User
{
public string UserName { get; set; }
public List<string> Roles { get; set; }
}
主要:
var users = new List<User>
{
new User { UserName = "Reza" , Roles = new List<string>{"Superadmin" } },
new User { UserName = "Amin" , Roles = new List<string>{"Guest","Reseption" } },
new User { UserName = "Nima" , Roles = new List<string>{"Nurse","Guest" } },
};
var query = users.SelectMany(user => user.Roles, (user, role) => new { user.UserName, role });
foreach (var obj in query)
{
Console.WriteLine(obj);
}
//output
//{ UserName = Reza, role = Superadmin }
//{ UserName = Amin, role = Guest }
//{ UserName = Amin, role = Reseption }
//{ UserName = Nima, role = Nurse }
//{ UserName = Nima, role = Guest }
您可以对序列的任何项使用操作
int[][] numbers = {
new[] {1, 2, 3},
new[] {4},
new[] {5, 6 , 6 , 2 , 7, 8},
new[] {12, 14}
};
IEnumerable<int> result = numbers
.SelectMany(array => array.Distinct())
.OrderBy(x => x);
//output
//{ 1, 2 , 2 , 3, 4, 5, 6, 7, 8, 12, 14 }
List<List<int>> numbers = new List<List<int>> {
new List<int> {1, 2, 3},
new List<int> {12},
new List<int> {5, 6, 5, 7},
new List<int> {10, 10, 10, 12}
};
IEnumerable<int> result = numbers
.SelectMany(list => list)
.Distinct()
.OrderBy(x=>x);
//output
// { 1, 2, 3, 5, 6, 7, 10, 12 }