简体   繁体   English

如何使用linq将Selectone行添加到下拉列表

[英]How to add Selectone row to a drop down list using linq

I have a table called States and i am displaying values of states in a dropdown list using linq query Now i want to insert selection on the first place of id 0. I am using following query to display dropdown list: 我有一个名为“状态”的表,我正在使用linq查询在下拉列表中显示状态值。现在,我想在id 0的第一位插入选择。我正在使用以下查询来显示下拉列表:

var str = (from li in dbContext.states

               where li.state >= 1
               select new
               {

                   a=li.state

               }).ToArray();

The above query gives mean all the values present in my DB.How can i insert select one at first place? 上面的查询给出了数据库中存在的所有值的平均值。我如何首先插入选择一个? Any one please suggest any method or syntax. 任何人请提出任何方法或语法。 Thanks 谢谢

You have to add one extra item to the beginning of the list. 您必须在列表的开头添加一个额外的项目。

var str = (from li in dbContext.states

               where li.state >= 1
               select new
               {

                   a=li.state

               }).ToList().Insert(0, "selectone");

See MajoBs answer. 请参阅MajoBs答案。

Although if you're using the @Html.DropDownListFor() Helper, you could also specify the Id 0 row using the optionLabelArgument: 尽管如果您使用@Html.DropDownListFor()帮助器,也可以使用@Html.DropDownListFor()指定ID 0行:

public static MvcHtmlString DropDownListFor<TModel, TProperty>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<TModel, TProperty>> expression,
    IEnumerable<SelectListItem> selectList,
    string optionLabel
)

Update: 更新:

I think the reason MajoB's example doesn't work is because you are using an anonymous type in the Linq query, but define a string in the .Insert() statement. 我认为MajoB的示例不起作用的原因是因为您在Linq查询中使用了匿名类型,但是在.Insert()语句中定义了一个字符串。

I believe it is not possible to reuse the same anonymous type that was used in the LINQ query in the .Insert() statement. 我相信不可能重用.Insert()语句中LINQ查询中使用的匿名类型。

So you would have to use a regular type. 因此,您将不得不使用常规类型。

(from x in 
    (from li in dContext.states
    where li.state >= 0
        select new {
            value = li.state,
            text = li.something
        }).AsEnumerable()
    select new SelectListeItem {
        Value = x.value.ToString()
        Text = x.text
    }).ToList().Insert(0, new SelectListItem() { .. });

The nested query is necessary because the .ToString() can't be translated to sql. 嵌套查询是必需的,因为.ToString()无法转换为sql。 The .AsEnumerable() call created an in-memory list where .ToString will work on. .AsEnumerable()调用创建了一个内存列表,在其中可以使用.ToString

I am actually using a Extension method like this in our code base. 我实际上在我们的代码库中使用了这样的扩展方法。 But it requires LinqKit to work. 但这需要LinqKit起作用。

public static IList<SelectListItem> ToSelectList<T>(this IQueryable<T> query
        , Expression<Func<T, long>> value
        , Expression<Func<T, string>> text
        , string nullText = null)
    {
        var result = (from x in
                          (from y in query.AsExpandable()
                           select new
                           {
                               Value = value.Invoke(y),
                               Text = text.Invoke(y),
                           }).AsEnumerable()
                      select new SelectListItem
                      {
                          Value = x.Value.ToString(),
                          Text = x.Text.ToString()
                      }).ToList();

        AddNullText(nullText, result);
        return result;
    }

private static void AddNullText(string nullText, List<SelectListItem> result)
    {
        if (nullText != null)
        {
            result.Insert(0, new SelectListItem()
            {
                Value = "",
                Text = nullText,
            });
        }
    }

(+ the same Extension method with Expression<Func<T, string>> , etc.. as second parameter.) (+同样的扩展方法,将Expression<Func<T, string>>等作为第二个参数。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM