Dan Hall
Dan Hall

Reputation: 1534

LINQ Select on string results in char

I have an array of objects with which I'm using LINQ to construct an object for a webservice, the problem I'm having is that where some of the data needs to be truncated under one of the selected values the select parameter is using a char instead of a string:

WebService.ORDER_WRITE_REQUEST WebServiceObject = new WebService.ORDER_WRITE_REQUEST();

WebServiceObject.ITEMS = array.Item
.Select(x => new WebService.ITEMS
{
Value1 = x.Units.UnitName.Where(x => x != null).Select(x => x.Length > 5 ? x.Substring(0, 5) : w);
}

The 'UnitName' value is a String but the parameter x ends up converting it to a char so I'm actually unable to use the substring method in the linq query.

I see the select is expecting it to be a char, but I'm sure i've done this before without issue. Does anyone have any idea how to resolve this?

Thanks.

Upvotes: 0

Views: 330

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460098

UnitName is a string which implements IEnumerable<char>, you're enumerating all chars.

You want this:

...
Value1 = x.Units
    .Where(x => x.UnitName != null)
    .Select(x => x.UnitName.Length > 5 ? x.UnitName.Substring(0, 5) : w);
...

Upvotes: 1

Related Questions