Add blank line to LINQ

  • Thread starter Thread starter Peter Morris
  • Start date Start date
P

Peter Morris

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
 
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?


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.
 
Back
Top