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.

Add blank line to LINQ

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter Morris - 07 Mar 2008 17:10 GMT
var editionList =
   from edition in software.Editions
   select new
       {
           ID = edition.ID,
           Name = edition.Name
       };

How can I ensure that the result starts with an "empty" row
   ID = -1,  Name = "<select>"

So that I can use this in a HTML drop down?

There's got to be a more simple way that what I am currently doing....

  var blankSoftware =
   from software in new string[] { "" }
   select new { ID = -1, Name = "" };
  var softwareList =
   from product in softwareRepository.GetAll()
   select new
    {
     ID = product.ID,
     Name = product.Name
    };
  ViewData["SoftwareList"] = blankSoftware.Union(softwareList);

Thanks

Pete
Jon Skeet [C# MVP] - 07 Mar 2008 18:33 GMT
> var editionList =
>     from edition in software.Editions
[quoted text clipped - 8 lines]
>
> So that I can use this in a HTML drop down?

After the above, use:

editionList = Enumerable.Repeat (new { ID=-1, Name="<select>" }, 1)
                       .Concat (editionList);

If you find yourself frequently doing this kind of thing, you might
like to write extension methods of Prepend and Append, which
prepend/append a single element of type T to an IEnumerable<T> - in
exactly the same way as the above.

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

Peter Morris - 07 Mar 2008 19:15 GMT
Lovely, you're a star!

Rate this thread:







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.