2

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 a 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. 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>?

Secondly, I have List<Users> and want to convert it into ICollection<Users> how achieve this?

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. These Users are also are contained inside Projects as collection. Every Project has its Users 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

Hassaan
  • 3,931
  • 11
  • 34
  • 67
  • 1
    What is `lstProjects`? If it's a `List` then you've *actually* got a `List` as `lstUsers`, despite your names. If you hover over the `var` of `var lstUsers` in Visual Studio, what does it show you? And what's the error message? It would *really* help if you'd show a short but complete program demonstrating the problem. (Also, why are you creating an empty list and then ignoring it? Why aren't you just using `var lstUsersToDisplay = lstUsers;`?) – Jon Skeet Jul 02 '14 at 20:43
  • @JonSkeet my appologize in typing basically i am retrieving the users collection from the project list. i have modified it – Hassaan Jul 02 '14 at 20:48
  • So your `users` range variable is actually a `Project`? Holy confusing naming, Batman! It sounds like you've now got a `List>`. Again, you should hover over the `var` to see what you've actually got... – Jon Skeet Jul 02 '14 at 20:49

3 Answers3

5

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

lstUsers isn't a List<User>. It's a 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:

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. You can assign that as is to a List<User> or an ICollection<User>

Servy
  • 202,030
  • 26
  • 332
  • 449
0
using System.Linq; //include extension method OfType<> for ICollection
...
List<Projects> userList = lstUsers.OfType<Projects>().ToList();
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
elpezganzo
  • 349
  • 4
  • 7