mvc LinqToSql 添加行“选择用户”对于下拉列表

发布于 2024-09-18 04:10:06 字数 1609 浏览 2 评论 0原文

我是 Linq 新手。我在网上搜索了又搜索解决方案,但找不到任何东西。我有一个 Linq 查询,我想在将一行(“选择用户”)传递到下拉列表之前将其插入到顶部。我一直在尝试使用 Union 但现在有用(它一直告诉我我的对象不支持 Union 方法)。在尝试插入行之前,我的代码非常简单。

    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;
    }

现在我尝试插入一行,我在互联网上找到了这个,似乎说他的解决方案可行。但它充满了错误。 http://magicode.wordpress.com/2009/08/20/inserting-an-item-in-iqueryable-object-using-union-method-and-linq/

我尝试使用以下代码,但无法编译。

    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;
    }

太累了,我快要瞎了。有什么帮助吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七婞 2024-09-25 04:10:06

您不需要触摸查询结果。您可以在下拉列表中添加默认选项“选择用户”。

试试这个:

我怎样才能将项目添加到 ASP.net MVC 中的 SelectList

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文