views:

54

answers:

1

I'm new to Linq. I have searched and searched the web for a solution, and can't find anything. I have a Linq query and I want to insert a row ("Select User") to the top before I pass it to the drop down list. I've been trying to use the Union but to now avail (it keeps telling me that my object doesn't support the Union method). My code, prior to attempting to insert a row, is very simple.

    public SelectList DropDown_Users()

    {
        var context = new VivarianDataContext();

        var query = from t in context.AspnetUsers
                    select new { t.UserId, t.LastName };

        list = new SelectList(query.AsEnumerable(), "UserId", "LastName");
        return list;
    }

Now I try to insert a row and I found this on the internet and it seems to say that his solution will work. But it is filled with errors. http://magicode.wordpress.com/2009/08/20/inserting-an-item-in-iqueryable-object-using-union-method-and-linq/

I tried to implement it using the following code, but it doesn't compile.

    public SelectList DropDown_Users()
    {
        SelectList list;

        //get the original data          
       var context = new SQL2005633131VivarianDataContext();
       var query = from t in context.AspnetUsers

        select new { t.UserId, t.LastName };

        //create a dummy table with an empty row
        var AllUsers = new List<AspnetUsers>();
        var BlankUser = new AspnetUsers()
            {UserId=System.Guid.Empty, LastName="Select One"};
        AllUsers.Add(BlankUser);   

        //use Union to join the data - ERRORS HERE - doesn't support Union
       var newTable = AllUsers.Union(query);


        list = new SelectList(newTable.AsEnumerable(), "UserId", "LastName");
        return list;
    }

So tired I'm going blind. Any help?

A: 

You don't need to touch the query result. You can add that default option "Select User" in the dropdownlist.

Try this:

How can I add an item to a SelectList in ASP.net MVC

Leniel Macaferi