Linq Conversion From ICollection<T> to List<T>
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. 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. 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.
Hassaan
I am just a techie and a long live learner. I love to work on new technologies.
Updated on June 04, 2022Comments
-
Hassaan almost 2 years
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. When performing the following query: (Note that
lstProjects
is aList<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. Like,var lstUsersToDisplay = new List<Users>(); lstUsersToDisplay = (List<Users>)lstUsers; //This can't be cast.
What's the approach to convert
ICollection<T>
toList<T>
?Secondly, I have
List<Users>
and want to convert it intoICollection<Users>
how achieve this?Edited: Scenario, more clearly is that All
Projects
are loaded inlstProjects
and we need to select theUsers
which were mapped to a specific project. These Users are also are contained insideProjects
as collection. EveryProject
has itsUsers collection
like if I decomposed the lstProjects it would be like:lstProjects --> [0]-->//other Properties ICollection[Users]-->[0]//Contains User class Properties [1].... [1] ... same procedure
Hope it clears the scenario
-
bdimag almost 10 yearsShould there be a WHERE in "Or in query expression syntax"?
-
Random Dev almost 10 yearsnice - but why don't you use the overload to
.Sinlge
that filters too in your 3rd snippet? It looks nicer and is more readable IMHO? -
Jon Skeet almost 10 years@bdimag: Doh, yes. Will fix.