(Please Select), Smith, Bob Jones, Sue .. how remove comma after (Please Select) in LINQ?

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

Ronald S. Cook

Hey guys,

The below code works fine, BUT appends a comma to my (Please Select).
Anybody know how I can get around this?

Thanks... and sorry is in VB.NET (I wish I was in C# on this project)... but
you guys are WAY more helpful than the VB forum.

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

Dim _Employee As New Employee()
_Employee.EmployeeLastName = "(Please Select)"
_Employee.EmployeeFirstName = ""
Dim _List As IList(Of Employee) = _Query.ToList()
_List.Insert(0, _Employee)

Dim _QueryWithSelect As IEnumerable = _
(From xyz In _List _
Select xyz.EmployeeId, _
EmployeeName = xyz.EmployeeLastName & ", " &
xyz.EmployeeFirstName).ToList()

Return _QueryWithSelect
 
Ronald S. Cook said:
The below code works fine, BUT appends a comma to my (Please Select).
Anybody know how I can get around this?

Sure. Currently you've got the following steps:

1) Build database query
2) Execute query and store results in list
3) Insert extra entry in list
4) Project list to strings
5) Convert to list
6) Return

Put step 3 after step 5 instead.
 
Thanks John! And Sloan, I did try to first conver the code to C# but the
converters out there aren't up on converting LINQ yet.

Man, I sure wish this project was in C#... mostly for 1) nicer flow of
syntax, and 2) support resources out there... especially LINQ sample code!
 
I mean't "Jon" not "John"... sorry.


Jon Skeet said:
Sure. Currently you've got the following steps:

1) Build database query
2) Execute query and store results in list
3) Insert extra entry in list
4) Project list to strings
5) Convert to list
6) Return

Put step 3 after step 5 instead.
 
Hi Jon.. sorry to bother one last time, but I think I did something wrong
with your re-ordering recommendation.

With the re-ording below, the list is returned, but doesn't contain the
(Please Select) record. Any last idea how to squeeze it in?

'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()

'4) Project list to strings.
'5) Convert to list.
Dim _QueryWithSelect As IEnumerable = _
(From xyz In _List _
Select xyz.EmployeeId, _
EmployeeName = xyz.EmployeeLastName & ", " & xyz.EmployeeFirstName).ToList()

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

'6) Return.
Return _QueryWithSelect

Thanks!
Ron
 
Hi Jon.. sorry to bother one last time, but I think I did something wrong
with your re-ordering recommendation.

With the re-ording below, the list is returned, but doesn't contain the
(Please Select) record. Any last idea how to squeeze it in?

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

Currently you're adding the dummy record into the intermediate list
*after* you've converted the list. You need to add it to the resulting
(string) list instead.

This bottom line should be

_QueryWithSelect.Insert(0, "(Please Select)")

and _QueryWithSelect should be declared as a List(Of String).

Jon
 
Thanks so much for the help, Jon. I made the changes you said (which make
sense), but then get this error:

Unable to cast object of type
'System.Collections.Generic.List'1[VB$AnonymousType_20'2[System.Guid,System]]'
to type System.Collections.Generic.IList'1[System.String]'.

Here is the code as I understand I should have it. I promise not to bother
you anymore!

'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()

'4) Project list to strings.
'5) Convert to list.
Dim _QueryWithSelect As IList(Of String) = _
(From xyz In _List _
Select xyz.EmployeeId, _
EmployeeName = xyz.EmployeeLastName & ", " & xyz.EmployeeFirstName).ToList()

'3) Insert extra entry in list.
_QueryWithSelect.Insert(0, "(Please Select)")

'6) Return.
Return _QueryWithSelect
 
Ronald S. Cook said:
Thanks so much for the help, Jon. I made the changes you said (which make
sense), but then get this error:

Unable to cast object of type
'System.Collections.Generic.List'1[VB$AnonymousType_20'2[System.Guid,System]]'
to type System.Collections.Generic.IList'1[System.String]'.

Here is the code as I understand I should have it. I promise not to bother
you anymore!

Ah, sorry, with the VB syntax I hadn't noticed that the projection was
to an anonymous type.

You'll need to create another instance of that anonymous type for the
"Please Select" entry, and make _QueryWithSelect implicitly typed. I
don't know how to do that in VB, I'm afraid.
 
Jon said:
Ronald S. Cook said:
Thanks so much for the help, Jon. I made the changes you said
(which make sense), but then get this error:

Unable to cast object of type
'System.Collections.Generic.List'1[VB$AnonymousType_20'2[System.Guid,System]]'
to type System.Collections.Generic.IList'1[System.String]'.

Here is the code as I understand I should have it. I promise not to
bother you anymore!

Ah, sorry, with the VB syntax I hadn't noticed that the projection was
to an anonymous type.

You'll need to create another instance of that anonymous type for the
"Please Select" entry, and make _QueryWithSelect implicitly typed. I
don't know how to do that in VB, I'm afraid.

Or just use Array.ConvertAll instead of Linq. Is there a new extension
method that makes ConvertAll available for a List perhaps?
 
Ben Voigt said:
Or just use Array.ConvertAll instead of Linq. Is there a new extension
method that makes ConvertAll available for a List perhaps?

List<T>.ConvertAll has been available since .NET 2.0. However, I don't
think that's the issue - the issue is adding the "(Please Select)"
*after* the projection to an anonymous type.
 
Jon Skeet said:
List<T>.ConvertAll has been available since .NET 2.0. However, I don't
think that's the issue - the issue is adding the "(Please Select)"
*after* the projection to an anonymous type.

LINQ isn't helpful for the stringification, though, in this case.

IEnumerable<Employee> query = From e In dc.Employees Where
e.Lookup.LookupCode <> "INA" Order By e.EmployeeLastName,
e.EmployeeFirstName;
List<Employee> listEmp = New List<Employee>(query)
List<string> listStrings = listEmp.ConvertAll(e => e.EmployeeLastName + ", "
+ e.EmployeeFirstName);
listStrings.Insert(0, "(Please Select)");
 
Ben Voigt said:
LINQ isn't helpful for the stringification, though, in this case.

IEnumerable<Employee> query = From e In dc.Employees Where
e.Lookup.LookupCode <> "INA" Order By e.EmployeeLastName,
e.EmployeeFirstName;
List<Employee> listEmp = New List<Employee>(query)

That's more simply done with ToList() if you really need a list - but
you don't, here.
List<string> listStrings = listEmp.ConvertAll(e => e.EmployeeLastName + ", "
+ e.EmployeeFirstName);
listStrings.Insert(0, "(Please Select)");

But the point is that I was mistaken in my original assumption that
this was projecting to strings - it isn't. It's converting to a list of
anonymous type instances, which is why my original suggestion didn't
work.

However, if it *did* use strings, it could all be done without
ConvertAll:

var strings = Enumerable.Repeat("(Please Select)", 1)
.Concat(query.Select
(e => e.EmployeeLastName + ", " + e.EmployeeFirstName));
 
Back
Top