ToLookup

//构造泛型列表
List<UserInfo> users = new List<UserInfo> {
new UserInfo{UserCode=1, UserName="User001", Password="001"},
new UserInfo{UserCode=1, UserName="User002", Password="002"},
new UserInfo{UserCode=2, UserName="User003", Password="003"},
new UserInfo{UserCode=2, UserName="User004", Password="004"}
};
//使用LINQ查找用户代码小于3的列表
//此时的query变量的类型是IEnumerable<UserInfo>类型
var query = from item in users
where item.UserCode < 3
select item;
//使用ToLookup方法将query转换为一对多字典类型
ILookup<int,UserInfo> userLookup = query.ToLookup(itm => itm.UserCode);

Response.Write("ILookup的结果是:<br/>");
foreach (var user in userLookup)
{
Response.Write(user.Key);
Response.Write(" ");
foreach (var user2 in user)
{
Response.Write(user2.UserName + " , ");
}
Response.Write("<br/>");
}

 

public class UserInfo
{
public UserInfo()
{
//
//TODO: 在此处添加构造函数逻辑
//
}

public int UserCode { get; set; }

public string UserName { get; set; }

public string Password { get; set; }

}

你可能感兴趣的:(UP)