Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ronald S. Cook - 28 Feb 2008 22:35 GMT
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
sloan - 28 Feb 2008 22:47 GMT
This is the C# group.

Either go with the

.general
or
.languages.vb

> Hey guys,
>
[quoted text clipped - 23 lines]
>
>        Return _QueryWithSelect
Jon Skeet [C# MVP] - 28 Feb 2008 23:24 GMT
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Ronald S. Cook - 29 Feb 2008 01:37 GMT
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!

>> The below code works fine, BUT appends a comma to my (Please Select).
>> Anybody know how I can get around this?
[quoted text clipped - 9 lines]
>
> Put step 3 after step 5 instead.
Ronald S. Cook - 29 Feb 2008 01:37 GMT
I mean't "Jon" not "John"... sorry.

>> The below code works fine, BUT appends a comma to my (Please Select).
>> Anybody know how I can get around this?
[quoted text clipped - 9 lines]
>
> Put step 3 after step 5 instead.
Ronald S. Cook - 29 Feb 2008 16:36 GMT
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

>> The below code works fine, BUT appends a comma to my (Please Select).
>> Anybody know how I can get around this?
[quoted text clipped - 9 lines]
>
> Put step 3 after step 5 instead.
Jon Skeet [C# MVP] - 29 Feb 2008 16:46 GMT
> 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?

<snip>

> '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
Ronald S. Cook - 29 Feb 2008 17:11 GMT
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

>> Hi Jon.. sorry to bother one last time, but I think I did something wrong
>> with your re-ordering recommendation.
[quoted text clipped - 21 lines]
>
> Jon
Jon Skeet [C# MVP] - 29 Feb 2008 19:05 GMT
> Thanks so much for the help, Jon.  I made the changes you said (which make
> sense), but then get this error:
[quoted text clipped - 5 lines]
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Ben Voigt [C++ MVP] - 06 Mar 2008 19:12 GMT
>> Thanks so much for the help, Jon.  I made the changes you said
>> (which make sense), but then get this error:
[quoted text clipped - 12 lines]
> "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?
Jon Skeet [C# MVP] - 06 Mar 2008 19:22 GMT
> > 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?

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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Ben Voigt [C++ MVP] - 06 Mar 2008 20:36 GMT
>> > You'll need to create another instance of that anonymous type for the
>> > "Please Select" entry, and make _QueryWithSelect implicitly typed. I
[quoted text clipped - 6 lines]
> 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)");
Jon Skeet [C# MVP] - 06 Mar 2008 20:43 GMT
> >> > You'll need to create another instance of that anonymous type for the
> >> > "Please Select" entry, and make _QueryWithSelect implicitly typed. I
[quoted text clipped - 13 lines]
> 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));
                       
Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.