(Please Select), ... Adams, Jon ... Baker Sue ... how remove comma after (Please Select) in LINQ?

  • Thread starter Thread starter Ronald S. Cook
  • Start date Start date
R

Ronald S. Cook

The below code works, but returns a list like:

(Please Select),
Adams, Jon
Baker, Sue
Davis, Don

.... i.e. there's a comma appended to (Please Select) that I'd really like to
get rid of.

Here's the code:

***************************************************
'1) Build database query.
Dim _Query As IEnumerable(Of Employee) = _
From e In dc.Employees _
Where e.Lookup.LookupCode <> "INA" _
Order By e.EmployeeLastName, _
e.EmployeeFirstName

'2) Execute query and store results in list.
Dim _List As IList(Of Employee) = _Query.ToList()

'3) Insert extra entry in list.
Dim _Employee As New Employee()
_Employee.EmployeeLastName = "(Please Select)"
_Employee.EmployeeFirstName = ""
_List.Insert(0, _Employee)

'4) Project list with concatenated field.
Dim _QueryWithSelect As IEnumerable = _
(From xyz In _List _
Select xyz.EmployeeId, _
EmployeeName = xyz.EmployeeLastName & ", " & _
xyz.EmployeeFirstName).ToList()

'5) Return.
Return _QueryWithSelect
***************************************************

I think I need to insert the (Please Select) record after the object with
the concatenated "EmployeeName" has been created, but I'm not sure how.

Thanks for any help,
Ron
 
Hello Ronald,
I'm not entirely sure I understand you.

I think you should not add "Please select" to the list at all.

It is better that the thing which uses you IEnumerable add this to whatever
interface you are providing.

Doe shtis make sense?
 
Even if I do it in the client, I still need to add that (Please Select) to
the top as is what is needed in our comboboxes.

Any idea how I can do it?

Thanks.
 
Hello Ronald,
Even if I do it in the client, I still need to add that (Please
Select) to the top as is what is needed in our comboboxes.

Any idea how I can do it?

Well you return the List/IEnumerable of the (Employee)items you would have
the client pick from.

You then interate through these items adding them to the combobox.

You can then insert the item you wish as an instruction as the first item.
(Although I would set selectedindex = -1 instead)

The key here is to seperate the additions as the comma is being added due
to processing which was meant for items which are of the Employee type. (Please
Select) is not a real employee and should not suffer the same processing.
 
Thanks Rory. Being new to LINQ, I've never iterated over an IEnumerble.
Can you give me any little snippet that could get me started?

Thanks!
Ron
 
Ronald:
Thanks Rory. Being new to LINQ, I've never iterated over an
IEnumerble. Can you give me any little snippet that could get me
started?

Not sure what you mean here. Linq isn't entirely relavent

You can for each through anything which is IEnumerable

For Each Item as Employee in SomeThingWhichIsIEnumberableOfEmployee
' Do Stuff with Item here. Probably add it to a ddList
Next
 
Back
Top