none
LINQ Concat RRS feed

  • Question

  • I have a dropdown list I'm populating with a LINQ query. the problem  arises , the drop down will raise an error when trying to bind  to a value not in the result set of the query.
    one of the possible values is 0, which is not going to be returned as part of the query to this table. What I would like to do is concat a row NOT belonging to a table to the result set.

    say the table is products. and you have productID and Description as the fields. 

    in SQL

    select productID, description from products
    union
    select 0,"none"

    How can I do this in LINQ
    Monday, July 13, 2009 4:01 PM

Answers

  • I think you will need to pull the data from the database for instance by doing ToList(), then you could use the Concat method to add a sequence with an object constructed on the client:

    var result = (from prod in dataContext.Products select new { ProductID = prod.ProductID, Description = prod.Description }).ToList().Concat(Enumerable.Repeat(new { ProductID = 0, Description = "none" }, 1));

    MVP XML My blog
    • Marked as answer by Stephen Ward Monday, July 13, 2009 7:32 PM
    Monday, July 13, 2009 5:37 PM

All replies

  • I think you will need to pull the data from the database for instance by doing ToList(), then you could use the Concat method to add a sequence with an object constructed on the client:

    var result = (from prod in dataContext.Products select new { ProductID = prod.ProductID, Description = prod.Description }).ToList().Concat(Enumerable.Repeat(new { ProductID = 0, Description = "none" }, 1));

    MVP XML My blog
    • Marked as answer by Stephen Ward Monday, July 13, 2009 7:32 PM
    Monday, July 13, 2009 5:37 PM
  • thanks for the help.

    now if we could just tell the dropdownlist to understand a null is vaild :)
    Monday, July 13, 2009 7:34 PM