score:11

Accepted answer

You are using SelectMany, which means project the individual items of another enumerable of enumerables. Company.Name I presume is a string, which is a collection of chars.

You probably want to just use Select given your expectations.

score:4

This is because of SelectMany, you need probably Select here.

SelectMany: Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence.

So executing SelectMany you get IEnumerable<char>, after you call ToList(), which projects it to a List<char>

score:3

There were a few other answers, but I thought I'd include an explanation.

If you look at the MSDN definition of a String, you'll see that it is implemented as an IEnumerable<char>:

[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class String : IComparable, 
    ICloneable, IConvertible, IComparable<string>, IEnumerable<char>, 
    IEnumerable, IEquatable<string>

Then, compare that to the definiton of SelectMany:

Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence

(there's a pretty good visual explanation here: http://www.codethinked.com/a-visual-look-at-the-linq-selectmany-operator)

Since the SelectMany will treat the String as an IEnumerable<char> object, we can infer that the resulting collection from .SelectMany(x => x.Name) will be an IEnumerable<char> made up of all of the characters contained within Name.

So, in this case, since you're really only looking for all of the Name strings contained within your a.Charge.Company objects, you really just need to use Select:

var CompanyNameList = Prop.ChargeItems.Select(a => a.Charge.Company.Name).ToList();

Related Articles