6

I have a linq query that contains

select new Project()
           {                             
               Manager =  list.Employee.Select(x => x.Manager).ToString()
           }).ToList();

This COMPILES, but I don't end up with the data... I get value for "Manager" of System.Linq.Enumerable+WhereSelectListIterator<Flex.Data.Systems,System.String> So I thought I would change to (string) instead of .ToString() but this does not work

select new Project()
           {                             
               Manager =  (string)list.Employee.Select(x => x.Manager)
           }).ToList();

Gives me the error message

Cannot convert type 'System.Collections.Generic.IEnumerable' to 'string'

I need it to be a collection, so that when I pass back out my List<Project> that Manager contains many Managers.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81

4 Answers4

11

What do you want Manager to be, a string with a comma separated list of values or a List<string>/String[]?

string:

Manager =  string.Join(",",list.Employee.Select(x => x.Manager))

collection:

Manager =   list.Employee.Select(x => x.Manager).ToList()
// or
Manager =   list.Employee.Select(x => x.Manager).ToArray()
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I need it to be a collection, so that when I pass back out my List that Manager contains many Managers , so the second part it what i need but Manager = list.Employee.Select(x => x.Manager).ToList() does not work as it gives me a Cannot implicitly convert type 'System.Collections.Generic.List' to 'string' –  May 13 '13 at 03:21
  • @corestone: then you have to change the type of your property from string to List. – Tim Schmelter May 13 '13 at 05:12
7

The problem is that list.Employee.Select(x => x.Manager) returns a collection of managers and property Manager is a string (?). You need some condition of which manager you want. For example append .First() to get the first one in the collection.

Magnus
  • 45,362
  • 8
  • 80
  • 118
  • He hasn't meenioned that it's a `string` property, although it's very likely since he uses `ToString`. – Tim Schmelter May 12 '13 at 21:46
  • @TimSchmelter That is true, but I don't see any other reason for trying to cast it into a string. – Magnus May 12 '13 at 21:47
  • I see, I can get that to at least give result, but I want to definitely pass back a collection. So I don't really want to even map these directly as I'm realizing this "select new Project" is creating a single record and I just want to pass data into the Project List –  May 13 '13 at 03:25
  • @CoreStone Then you are going to have to change `Manager` in class `Project` from a string to a List and append a `.ToList()` to your expression. – Magnus May 13 '13 at 06:59
0

Try this worked for me :

 var returnCollection = (from manager in list.Employee 
     select manager).ToList();
Akxaya
  • 834
  • 8
  • 6
0

Whenever you make a selection of columns from a collection it will return a collection. Even if you specify a where clause to match an exact record.

For your case, you can have them converted to

Manager =   list.Employee.Select(x => x.Manager).ToList()

or

Manager =   list.Employee.Select(x => x.Manager).ToArray()

This is what Tim told above.

There could be case where you get a collection and you are sure that the first one is what you need. For that you can always use:

Manager =   list.Employee.Select(x => x.Manager).First();

Note: This might throw an error if there is nothing found as a collection along with where clause used. In such case use:

Manager =   list.Employee.Select(x => x.Manager).FirstOrDefault();

This will not give error even if noting is returned matching.

Saggy
  • 624
  • 8
  • 23