[英]Linq Conversion From ICollection<T> to List<T>
I am using Code First Entity Framework. 我正在使用代码优先实体框架。
I am using the following simple classes: 我正在使用以下简单的类:
public class Users
{
public ICollection<Projects> Projects{get;set;}
}
public class Projects
{
public ICollection<Users> Users{get;set;}
}
I am using linq for data retrieval. 我正在使用linq进行数据检索。 When performing the following query: (Note that
lstProjects
is a List<Project>
) 执行以下查询时:(请注意,
lstProjects
是List<Project>
)
var lstUsers = (from users in lstProjects
where users.ProjectId == pId
select users.Users).ToList();
I have a List<Users>
object and want to populate this List with items. 我有一个
List<Users>
对象,想用项目填充此List。 Like, 喜欢,
var lstUsersToDisplay = new List<Users>();
lstUsersToDisplay = (List<Users>)lstUsers; //This can't be cast.
What's the approach to convert ICollection<T>
to List<T>
? 将
ICollection<T>
转换为List<T>
什么?
Secondly, I have List<Users>
and want to convert it into ICollection<Users>
how achieve this? 其次,我有
List<Users>
并想将其转换为ICollection<Users>
怎么实现呢?
Edited: Scenario, more clearly is that All Projects
are loaded in lstProjects
and we need to select the Users
which were mapped to a specific project. 编辑:方案,更清楚的是,所有
Projects
都在加载lstProjects
,我们需要选择Users
,其被映射到具体的项目。 These Users are also are contained inside Projects
as collection. 这些用户也作为集合包含在
Projects
中。 Every Project
has its Users collection
like if I decomposed the lstProjects it would be like: 每个
Project
都有其Users collection
就像我分解lstProjects一样:
lstProjects --> [0]-->//other Properties ICollection[Users]-->[0]//Contains User class Properties [1].... [1] ... same procedure
Hope it clears the scenario 希望能解决这个问题
If your query is genuinely this: 如果您的查询是真的 :
var lstUsers = (from users in lstProjects
where users.ProjectId == pId
select users.Users).ToList();
then that's equivalent to: 那等于:
List<ICollection<Users>> lstUsers = (from users in lstProjects
where users.ProjectId == pId
select users.Users).ToList();
If you're trying to get the list of uses from a single project, I'd write that as: 如果你想从一个单一的项目使用的列表中,我会写为:
var lstUsers = lstProjects.Single(project => project.ProjectId == pId)
.Users
.ToList();
If there could be multiple projects with the same ProjectId
, you want to flatten the users. 如果可能有多个具有相同
ProjectId
,则您要使用户扁平化。 For example: 例如:
var lstUsers = lstProjects.Where(project => project.ProjectId == pId)
.SelectMany(project => project.Users)
.ToList();
Or in query expression syntax: 或在查询表达式语法中:
var lstUsers = (from project in lstProjects
where project.ProjectId == pId
from user in project.Users
select user).ToList();
Note the fact that my range variable is called project
, because it refers to a project, not a user. 请注意,我的范围变量称为
project
,因为它引用的是项目, 而不是用户。 Naming is important - pay attention to it. 命名很重要-请注意。 I would also rename the
Projects
and Users
types to just Project
and User
, assuming each is really only meant to represent a single entity. 我还要将
Projects
和Users
类型重命名为Project
和User
,假设每个类型实际上仅表示单个实体。
What's the approach to convert
ICollection<T>
toList<T>
?将
ICollection<T>
转换为List<T>
什么?
The most generic way is to use ToList
: 最通用的方法是使用
ToList
:
lstUsersToDisplay = lstUsers.ToList();
But it looks like you really have a List<ICollection<Users>>
. 但是看起来您确实有一个
List<ICollection<Users>>
。 To "flatten" that list use SelectMany
: 要“拉平”该列表,请使用
SelectMany
:
lstUsersToDisplay = lstUsers.SelectMany(c => c).ToList();
i have
List<Users>
and want to convert it intoICollection<Users>
how achieve this?我有
List<Users>
并想将其转换为ICollection<Users>
怎么实现呢?
A List<Users>
is already an ICollection<Users>
- no conversion is necessary. List<Users>
已经是ICollection<Users>
-无需转换。
lstUsers
isn't a List<User>
. lstUsers
不是List<User>
。 It's a List<ICollection<User>>
. 这是一个
List<ICollection<User>>
。 You map each project to a sequence of users, not to a single user. 您将每个项目映射到一系列用户,而不是单个用户。 To flatten a collection of collections into just a collection of the inner items you would use
SelectMany
, or, in query syntax, you'd write out your query like so: 要将集合的集合平整为仅内部项目的集合,可以使用
SelectMany
,或者使用查询语法,您可以像这样写出查询:
var lstUsers = (from project in lstProjects
where project.ProjectId == pId
from user in project.Users
select user).ToList();
Now you have a List<User>
for lstUsers
. 现在,您有了
lstUsers
的List<User>
。 You can assign that as is to a List<User>
or an ICollection<User>
您可以按原样将其分配给
List<User>
或ICollection<User>
using System.Linq; //include extension method OfType<> for ICollection
...
List<Projects> userList = lstUsers.OfType<Projects>().ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.